简体   繁体   English

使用Linq从XML读取并将其添加到包含对象的列表中

[英]Read from XML Using Linq and add it to a list containing objects

I'm trying to read from an XML file and add every contact to the contacts list the error i get is that can't convert List to List 我正在尝试从XML文件读取并将每个联系人添加到联系人列表,但我得到的错误是无法将列表转换为列表

writes an xml file for each contact in contacts (Works) 为联系人中的每个联系人编写一个xml文件(Works)

var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    var xEle = new XElement("Contact",
        from contact in contacts
        select new XElement("Contact",
            new XAttribute("Name", contact.Name),
                new XElement("Address", contact.StreetAddress),
                new XElement("AreaCode", contact.AreaCode),
                new XElement("City", contact.City),
                new XElement("Phone", contact.Phone),
                new XElement("Email", contact.Email)
                ));
        xEle.Save(path + "\\contacts.xml");

Reads from the xml and adds it to the list (DOES NOT WORK!) 从xml读取并将其添加到列表中(不起作用!)

XDocument xmlDoc = XDocument.Load(path + "\\contacts.xml");
contacts = (List<Contact>)xmlDoc.Descendants("Contact");

XML File Output XML文件输出

<Contacts>
    <Contact Name="Nils Nilsson">
        <Address>MyStreet 17B</Address>
        <AreaCode>55555</AreaCode>
        <City>BigCity</City>
        <Phone>0705555555</Phone>
        <Email>mail@gmail.com</Email>
    </Contact>
</Contact>

Contact Class 接触类

 public class Contact
    {
        public string Name { get; set; }

        public string StreetAddress { get; set; }

        public string AreaCode { get; set; }

        public string City { get; set; }

        public string Phone { get; set; }

        public string Email { get; set; }
    }

My existing list 我现有的清单

List<Contact> contacts = new List<Contact>();

As pointed out, the reason your original code doesn't work is that all queries in LINQ to XML return some form of XObject - most commonly an XAttribute or XElement . 正如指出的那样,你的原始代码不起作用的原因是LINQ to XML中的所有查询返回某种形式的XObject -最常见的XAttributeXElement It won't map anything to Contact . 它不会将任何内容映射到Contact

The easiest way to map your XML to your objects is to make use of built in support for XML Serialization. 将XML映射到对象的最简单方法是利用对XML序列化的内置支持。 You class needs a couple of attributes & an extra class to map to your XML structure: 您的类需要几个属性和一个额外的类才能映射到您的XML结构:

[XmlRoot("Contacts")]
public class Contacts : List<Contact>
{

}

public class Contact
{
    [XmlAttribute]
    public string Name { get; set; }

    [XmlElement("Address")]
    public string StreetAddress { get; set; }

    public string AreaCode { get; set; }

    public string City { get; set; }

    public string Phone { get; set; }

    public string Email { get; set; }
}

And then read your XML as follows: 然后按如下所示读取您的XML:

using (var reader = XmlReader.Create(Path.Combine(path, "contacts.xml"))
{
    var serializer = new XmlSerializer(typeof(Contacts));
    var contacts = (Contacts)serializer.Deserialize(reader);
}

Note you can also use the Serialize method to create the XML in the first instance. 请注意,您还可以在第一个实例中使用Serialize方法创建XML。

You can try something like this: 您可以尝试如下操作:

var xmlDoc = XDocument.Load(path + "\\contacts.xml");
var xElements = xmlDoc.Descendants("Contact");
var xmlSerializer = new XmlSerializer(typeof(Contact));
contacts = xElements.Select(xe => (Contact)xmlSerializer.Deserialize(xe.CreateReader())).ToList();

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

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