日期:2014-05-18  浏览次数:21188 次

关于写XML文件
public static void write()
  {
  XmlWriter writer = XmlWriter.Create("E:\\newbook.xml");
  //写根元素开始标签
  writer.WriteStartElement("books");
  //写入第一本书数据
  writer.WriteStartElement("lovel");
  writer.WriteElementString("name", "约翰·克里斯朵夫");
  writer.WriteElementString("author", "罗曼·罗兰");
  writer.WriteEndElement();
  //写入第二本书数据
  writer.WriteStartElement("lovel");
  writer.WriteElementString("name", "瓦尔登湖");
  writer.WriteElementString("author", "梭罗");
  writer.WriteEndElement();
  //写根元素结束标签
  writer.WriteEndElement();
  writer.Flush();
  }


上面的程序运行之后生成的XML文件是下面的格式:
<?xml version="1.0" encoding="utf-8"?><books><lovel><name>约翰·克里斯朵夫</name><author>罗曼·罗兰</author></lovel><lovel><name>瓦尔登湖</name><author>梭罗</author></lovel></books>

怎样生成格式规范的XML文件????

理想格式是下面的样子:
<?xml version="1.0" encoding="utf-8"?>
<books>
  <lovel>
  <name>约翰·克里斯朵夫</name>
  <author>罗曼·罗兰</author>
  </lovel>
  <lovel>
  <name>瓦尔登湖</name>
  <author>梭罗</author>
  </lovel>
</books>

------解决方案--------------------
这两个格式一样啊,你贴错了吧
------解决方案--------------------
先帮你顶着吧,两个格式没什么差别.
------解决方案--------------------
在我这显示如下


<?xml version="1.0" encoding="utf-8" ?> 
- <books>
- <lovel>
<name>JONE</name> 
<author>ReS</author> 
</lovel>
- <lovel>
<name>Tim</name> 
<author>Sues</author> 
</lovel>
</books>
------解决方案--------------------
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create("e:\\11111.xml", settings);

// Write the book element.
writer.WriteStartElement("book");

// Write the title element.
writer.WriteStartElement("title");
writer.WriteString("");
writer.WriteEndElement();

// Write the close tag for the root element.
writer.WriteEndElement();

// Write the XML and close the writer.
writer.Close();

------解决方案--------------------
C# code

//添加这些
XmlWriterSettings settings = new XmlWriterSettings(); 
settings.Indent = true;  //缩进属性设为正确
settings.OmitXmlDeclaration = true; //忽略XML声明。
XmlWriter writer = XmlWriter.Create("e:\\11111.xml", settings); //注意第二个参数

------解决方案--------------------
学习
------解决方案--------------------
6,7楼都给出了。
设置Indent就行了
------解决方案--------------------
mark,来晚了.