English 中文(简体)
XML 与林克交接
原标题:XML Parsing With Linq

I Want to Parse XML with Linq.

这里是我的XML数据。

 <?xml version="1.0" encoding="utf-8" ?>
    <people>
      <person>
        <firstname>Kate</firstname>
        <lastname>Smith</lastname>
        <Address>Address</Address>
        <Address>Address2</Address>
        <Address>Address3</Address>
        <age>27</age>
      </person>
      <person>
        <firstname>Tom</firstname>
        <lastname>Brown</lastname>
        <Address>Address4</Address>
        <Address>Address5</Address>
        <Address>Address6</Address>
        <age>30</age>
      </person>
      <person>
        <firstname>Tim</firstname>
        <lastname>Stone</lastname>
        <Address>Address7</Address>
        <Address>Address8</Address>
        <Address>Address9</Address>
        <age>36</age>
      </person>
      <person>
        <firstname>Ann</firstname>
        <lastname>Peterson</lastname>
        <Address>Address10</Address>
        <Address>Address11</Address>
        <Address>Address12</Address>
        <age>27</age>
      </person>
    </people>

public class XmlParser
    {
        List<Person> List = new List<Person>();
        public XmlParser()
        {
            XDocument loadedData = XDocument.Load("Persons.xml");
            var data = (from query in loadedData.Descendants("person")
                select new Person
                {
                    FirstName = (string)query.Element("firstname"),
                    LastName = (string)query.Element("lastname"),
                    //Address = (List<string>)query.Element("Address"), // I Want To Load All Address Into My List
                    Age = (int)query.Element("age")
                }).ToList();
            List.AddRange(data);
        }

        public class Person
        {
            string firstname;
            string lastname;
            List<string> address;
            int age;

            public string FirstName
            {
                get { return firstname; }
                set { firstname = value; }
            }

            public string LastName
            {
                get { return lastname; }
                set { lastname = value; }
            }

            public List<string> Address
            {
                get { return address; }
                set { address = value; }
            }

            public int Age
            {
                get { return age; }
                set { age = value; }
            }

        }
    }

纽 约 请帮助研究如何将Adrress Into List in LINQ Query。 As You Can See There Are many Address Nodes Under The Person. 因此,我怎么能把所有地址都列入我的名单目标。

最佳回答

获取要素清单的一个途径是,在你最初的林克询问中再打一盘。

var loadedData = XDocument.Load("Persons.xml");
var data = (from query in loadedData.Descendants("person")
            select new Person
            {
                FirstName = (string)query.Element("firstname"),
                LastName = (string)query.Element("lastname"),
                Address = (from address in query.Elements("Address") 
                           select address.Value).ToList(),
                Age = (int)query.Element("age")
            });

This pattern occurs all the time in linq to xml so i would remember it.

问题回答

暂无回答




相关问题
how to represent it in dtd?

I have two element action and guid. guid is a required field when action is add. but when action is del it will not appear in file. How to represent this in dtd ?

.Net application configuration add xml-data

I need to add xml-content to my application configuration file. Is there a way to add it directly to the appSettings section or do I need to implement a configSection? Is it possible to add the xml ...

XStream serializing collections

I have a class structure that I would like to serialize with Xstream. The root class contains a collection of other objects (of varying types). I would like to only serialize part of the objects that ...

MS Word splits words in its XML format

I have a Word 2003 document saved as a XML in WordProcessingML format. It contains few placeholders which will be dynamically replaced by an appropriate content. But, the problem is that Word ...

Merging an XML file with a list of changes

I have two XML files that are generated by another application I have no control over. The first is a settings file, and the second is a list of changes that should be applied to the first. Main ...

How do I check if a node has no siblings?

I have a org.w3c.dom.Node object. I would like to see if it has any other siblings. Here s what I have tried: Node sibling = node.getNextSibling(); if(sibling == null) return true; else ...

Ordering a hash to xml: Rails

I m building an xml document from a hash. The xml attributes need to be in order. How can this be accomplished? hash.to_xml

热门标签