使用 C# 将 XML 文件写入特定的 XML 结构
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10809724/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Writing a XML file with C# to a specific XML structure
提问by Anuya
The Usual way, i code to write a XML file is,
通常,我编写 XML 文件的代码是,
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
XmlWriter writer = XmlWriter.Create("Products.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Product");
writer.WriteAttributeString("ID", "001");
writer.WriteAttributeString("Name", "Keyboard");
writer.WriteElementString("Price", "10.00");
writer.WriteStartElement("OtherDetails");
writer.WriteElementString("BrandName", "X Keyboard");
writer.WriteElementString("Manufacturer", "X Company");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
But the above code gives me a different XML structure, How to code if i need the output as below given structure,
但是上面的代码给了我一个不同的 XML 结构,如果我需要输出如下给定的结构,如何编码,
<Books>
<Book ISBN="0553212419">
<title>Sherlock Holmes</title>
<author>Sir Arthur Conan Doyle</author>
</Book>
<Book ISBN="0743273567">
<title>The Great Gatsby</title>
<author>F. Scott Fitzgerald</author>
</Book>
<Book ISBN="0684826976">
<title>Undaunted Courage</title>
<author>Stephen E. Ambrose</author>
</Book>
<Book ISBN="0743203178">
<title>Nothing Like It In the World</title>
<author>Stephen E. Ambrose</author>
</Book>
</Books>
Thanks
谢谢
采纳答案by pstrjds
As was commented, just modify the code you already have to write the correct elements.
正如所评论的,只需修改您已经必须编写正确元素的代码。
XmlWriter writer = XmlWriter.Create(@"Products.xml", settings);
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Books");
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", "0553212419");
writer.WriteElementString("Title", "Sherlock Holmes");
writer.WriteElementString("Author", "Sir Arthur Conan Doyle");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
Wash, rinse, repeat. I would recommend writing a method to add each book.
清洗,冲洗,重复。我建议编写一种方法来添加每本书。
EDIT- method for writing books
编辑- 写书的方法
void WriteBookData(XmlWriter writer, string isbn, string title, string author)
{
writer.WriteStartElement("Book");
writer.WriteAttributeString("ISBN", isbn);
writer.WriteElementString("Title", title);
writer.WriteElementString("Author", author);
writer.WriteEndElement();
}
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(@"Products.xml", settings))
{
writer.WriteStartDocument();
writer.WriteComment("This file is generated by the program.");
writer.WriteStartElement("Books");
WriteBookData(writer, "0553212419", "Sherlock Holmes", "Sir Arthur Conan Doyle");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
回答by praveen
Why don't you use Linq to XML .Its pretty straightforward and very easy to undestand
你为什么不使用 Linq to XML 。它非常简单而且很容易理解
string filename = string.Empty;
XElement res= new XElement ("Books",new XElement ("Book",new XAttribute ("ISBN","="+055321212),
new XElement ("Title","Sherlock Homes"),
new XElement ("author","Sir Arthur Conan")
)
);
res.Save(filename);
you can use foreach look to store the hard coded values in the above statements
您可以使用 foreach 查找存储上述语句中的硬编码值

