简体   繁体   English

使用LINQ将CLR对象写入XML

[英]Write CLR objects to XML with LINQ

I have an ObservableCollection items. 我有一个ObservableCollection项目。 I want to convert these to XML format so that I can store them for later use. 我想将它们转换为XML格式,以便可以存储它们以备后用。 I'm a little new to LINQ and I'm not sure how to do this. 我对LINQ有点陌生,我不确定该怎么做。 I know that I need to rely on the XDocument. 我知道我需要依靠XDocument。 But I'm not sure how to execute the query and get the results into a string format. 但是我不确定如何执行查询并将结果转换为字符串格式。

Can somebody please provide some pointers for me? 有人可以为我提供一些建议吗? It just seems like such an easy task, I'd be surprised if it couldn't be done. 看来这是一件容易的事,如果无法完成,我会感到惊讶。

Thank you, 谢谢,

You need Linq to XML . 您需要Linq到XML I can't post a real code here since I don't know the structure of your data, but here's a dummy example: 我不知道您的数据结构,因此无法在此处发布真实的代码,但这是一个虚拟示例:

List<Person> people = ...

var doc = new XDocument(
            new XElement("People",
                from p in people
                select new XElement("Person",
                    new XAttribute("Id", p.Id),
                    new XElement("LastName", p.LastName),
                    new XElement("FistName", p.FirstName))));

doc.Save("people.xml");

Note that Linq is not the only option here. 请注意,Linq不是这里唯一的选择。 Another very good approach is XML serialization. 另一个非常好的方法是XML序列化。

Using the DataContractSerializer is your best bet. 最好使用DataContractSerializer。 It's designed to do exactly what you are asking for. 它旨在完全满足您的要求。

http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datacontractserializer(v=VS.95).aspx http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.datacontractserializer(v=VS.95).aspx

Advantages over XMLSerializer: 与XMLSerializer相比的优势:

  1. Opt-in rather than opt-out properties to serialize. 选择启用而不是选择退出属性进行序列化。 This mean you specify what you want serialize 这意味着您指定要序列化的内容

  2. Because it is opt in you can serialize not only properties, but also fields. 因为它是可选的,所以您不仅可以序列化属性,还可以序列化字段。 You can even serialize non-public members such as private or protected members. 您甚至可以序列化非公共成员,例如私有或受保护的成员。 And you dont need a set on a property either (however without a setter you can serialize, but not deserialize) 而且您也不需要在属性上进行设置(但是,如果没有设置器,您可以序列化但不能反序列化)

  3. Is about 10% faster than XmlSerializer to serialize the data because since you don't have full control over how it is serialize, there is a lot that can be done to optimize the serialization/deserialization process. 序列化数据的速度比XmlSerializer快10%,这是因为由于您无法完全控制数据的序列化方式,因此可以做很多事情来优化序列化/反序列化过程。

  4. Can understand the SerializableAttribute and know that it needs to be serialized 可以理解SerializableAttribute并知道它需要被序列化

  5. More options and control over KnownTypes 更多选项和对KnownTypes的控制

Disadvantages: 缺点:

  1. No control over how the object is serialized outside of setting the name and the order 除了设置名称和顺序之外,无法控制对象的序列化方式

I'm not sure what you want. 我不确定你想要什么。 You can create a XElement and convert it to a string with the ToString method. 您可以创建一个XElement并使用ToString方法将其转换为字符串。 You can do the same with XDocument. 您可以使用XDocument进行相同的操作。

I'm not at all familiar with Silverlight, but it sounds like you are going to need to use XML serialization. 我对Silverlight一点都不熟悉,但是听起来您将需要使用XML序列化。 XML serialization allows you to serialize an object into an XML representation, which you can then later deserialize from XML back into an object. XML序列化允许您将对象序列化为XML表示形式,然后可以随后将其从XML反序列化为对象。

Here are some tutorials and information on XML serialization in .NET: 这是有关.NET中XML序列化的一些教程和信息:

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

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