简体   繁体   English

XmlTextWriter是写Xml数据的最佳方法吗

[英]Is XmlTextWriter The Best Way To Write Out Xml Data

I have a list of products I want to generate xml for currently all the products are held in a List<> object.... 我有一个要生成xml的产品列表,当前所有产品都保存在List <>对象中。

 List<Product> products= DataAccess.GetProducts();
            XmlTextWriter writer = new XmlTextWriter(@"C:\ProductsToXml.xml", null);

            writer.WriteComment("Nick's Test Products To Xml.");

            writer.WriteStartElement("xml");

            writer.WriteStartElement("Product");
            writer.WriteStartElement("Name");
            writer.WriteString("Almonds");
            writer.WriteEndElement();

            //end for product
            writer.WriteEndElement();

            //end for xml
            writer.WriteEndElement();
            writer.Close();

Now I was gonna use this skeleton and loop through all the products and add xml like the skeleton shows above. 现在,我将使用此框架并遍历所有产品,并像上面显示的框架一样添加xml。 The thing is there is gonna be alot of writer.WriteStartElement instructions because my products have alot of data in them. 问题是会有很多writer.WriteStartElement指令,因为我的产品中有很多数据。 I am not sure if this is the best way to create an xml file or does it even matter? 我不确定这是创建xml文件的最佳方法还是无关紧要? Is this approach ok for what I am doing? 这种方法适合我的工作吗? Basically taking a product object that has a bunch of properties in it and creating xml out of it thats all the purpose of this code is for. 基本上,获取其中具有一堆属性的产品对象,并从中创建xml,这就是该代码的全部用途。

Would you guys do it different? 你们会有所不同吗?

I think there are two options, one use a custom XmlSerializer for List<Product> or, more straightforward use Linq to XML to serialize your products: 我认为有两种选择,一种使用针对List<Product>的自定义XmlSerializer ,或者更直接地使用Linq到XML来序列化您的产品:

List<Product> products = new List<Product>();
products.Add(new Product() { Id =1, Name="Foo"});
products.Add(new Product() { Id =2, Name="Bar"});

XElement xDoc = new XElement("Products", products.Select(p => new XElement("Product", 
                                                         new XAttribute("id", p.Id), 
                                                         new XAttribute("name", p.Name))));
xDoc.Save(@"testOut.xml");

Output: 输出:

<?xml version="1.0" encoding="utf-8"?>
<Products>
  <Product id="1" name="Foo" />
  <Product id="2" name="Bar" />
</Products>

Edit: 编辑:

Composition is pretty easy in Linq to XML - so if I wanted an outer node of "Stuff" I can just prepend that node (and add a bracket at the end of course): 在Linq to XML中,组合非常容易-因此,如果我想要“ Stuff”的外部节点,我可以在该节点之前添加前缀(并在课程的最后添加括号):

XElement xDoc = new XElement("Stuff", 
                             new XElement("Products", products.Select(p => new XElement("Product", 
                                                                           new XAttribute("id", p.Id), 
                                                                           new XAttribute("name", p.Name)))));

you can just duplicate this approach for your "Categories" list, just have new XElement("Categories") ... and so on. 您可以将这种方法复制到“类别”列表中,只需使用new XElement("Categories") ...等等。

I have this in my code: 我的代码中有这个:

XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<Item>));
TextWriter textWriter = new StreamWriter(@"items.xml");
serializer.Serialize(textWriter, items);
textWriter.Close();

which creates an .xml files with the root of 'arrayofitems'. 这会创建一个以“ arrayofitems”为根的.xml文件。

How about making use of LINQ for serializing data? 如何使用LINQ序列化数据? There are several articles on the net. 网上有几篇文章。 Here is one. 这是一个。 http://kiranpatils.wordpress.com/2008/09/23/generic-list-to-xml-using-linq/ http://kiranpatils.wordpress.com/2008/09/23/generic-list-to-xml-using-linq/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM