Linq to XML(C#)解析没有属性/ id到对象的XML树

时间:2012-01-24 15:01:17

标签: c# xml linq

我有一个类(Person)和一个XML树:

<rows>
    <row>
        <number>1110</number>
        <name>Street</name>
        <value>First Avenue</value>
        <description>First Avenue</description>
    </row>
    <row>
        <number>1120</number>
        <name>House number</name>
        <value>281</value>
        <description>281</description>
    </row>
    <row>
        <number>1160</number>
        <name>Postal</name>
        <value>2552AD</value>
        <description>2552AD</description>
    </row>
    <row>
        <number>1170</number>
        <name>Area</name>
        <value>Peru</value>
        <description>Peru</description>
    </row>
</rows>

人员类具有街道,门牌号,邮政和地区等属性。如何填写此人物?

我的查询中的问题是我不知道在哪里添加:

where (string)c.Element("name") == "Street"

到目前为止我的代码:

public class XMLparser
{
    public Person XMLparse(string path)
    {
        Person person = new Person();
        List<Person> ListPerson = new List<Person>();
        XDocument file = XDocument.Load(path);
        var query = from c in file.Descendants("row")
                    select new
                    {
                        c.Element("value").Value 
                    };

        // Loop through results
        foreach (var value in query)
        {
            person._street = value.Value;
            person._housenumber = value.Value;
            person._postal = value.Value;
            person._area = value.Value;
        }

        return person;
   }
}

我想要的是从xml中获取并填入

person._street = value.Value;      //<-- First Avenue
person._housenumber = value.Value; //<-- 281
person._postal = value.Value;      //<-- 2552AD
person._area = value.Value;        //<-- Peru    

编辑:

这是原始的XMl树。

    <?xml version="1.0" encoding="utf-8" ?>
    <results>
      <person>
        <categories>
          <category>
            <number>01</number>
            <name>Person</name>
            <rows>
              <row>
                <number>0110</number>
                <name>ID</name>
                <value>value ID</value>
                <description>value ID</description>
              </row>
            </rows>
          </category>
          <category>
            <number>08</number>
            <name>Address</name>
            <rows>
              <row>
                <number>1110</number>
                <name>Street</name>
                <value>value street</value>
                <description>value street</description>
              </row>
              <row>
                <number>1120</number>
                <name>House number</name>
                <value>value House number</value>
                <description>value House number</description>
              </row>
              <row>
                <number>1160</number>
                <name>Postal</name>
                <value>value Postal</value>
                <description>value Postal</description>
              </row>
              <row>
                <number>1170</number>
                <name>Area</name>
                <value>value Area</value>
                <description>value Area</description>
              </row>
            </rows>
          </category>
        </categories>
      </person>
      <person>
        <categories>
          <category>
            <number>01</number>
            <name>Person</name>
            <rows>
              <row>
                <number>0110</number>
                <name>ID</name>
                <value>value ID</value>
                <description>value ID</description>
              </row>
            </rows>
          </category>
          <category>
            <number>08</number>
            <name>Address</name>
            <rows>
              <row>
                <number>1110</number>
                <name>Street</name>
                <value>value Street</value>
                <description>value Street</description>
              </row>
              <row>
                <number>1120</number>
                <name>House number</name>
                <value>value House number</value>
                <description>value House number</description>
              </row>
              <row>
                <number>1130</number>
                <name>House number extra</name>
                <value>value House number extra</value>
                <description>value House number extra</description>
              </row>
              <row>
                <number>1160</number>
                <name>Postal</name>
                <value>value Postal</value>
                <description>value Postal</description>
              </row>
              <row>
                <number>1170</number>
                <name>Area</name>
                <value>value Area</value>
                <description>value Area</description>
              </row>
            </rows>
          </category>
        </categories>
      </person>
    </results>

3 个答案:

答案 0 :(得分:1)

您可以使用此代码:

XDocument file = XDocument.Parse(xml);
var persons
  = file.Descendants("rows")
        .Select (x => 
            {
                var person = new Person();
                var values = x.Descendants("row")
                              .GroupBy(y => y.Element("name").Value, 
                                       y => y.Element("value").Value)
                              .ToDictionary (y => y.Key, y => y.First());

                foreach(var value in values)
                {
                    switch (value.Key)
                    {
                        case "Street":
                            person.Street = value.Value;
                            break;
                        case "House number":
                            person.Housenumber = value.Value;
                            break;
                        case "Postal":
                            person.Postal = value.Value;
                            break;
                        case "Area":
                            person.Area = value.Value;
                            break;
                    }
                }
                return person;
            });

它假定XML可以包含多个rows标记,每个标记一个。然后,它会获取row代码中每个rows代码的名称和值,并使用这些值初始化一个人。

答案 1 :(得分:0)

您的Linq查询需要包含“naam”值和“value”值。然后在foreach循环中为每个预期的“naam”值添加一个带有大小写块的开关块。然后将“value”值存储在Person对象中。

答案 2 :(得分:-1)

以下代码应该为您提供人物对象列表:

string sampleXml =
            @"<rows>
      <row>
        <number>1110</number>
        <name>Street</name>
        <value>First Avenue</value>
        <description>First Avenue</description>
      </row>
      <row>
        <number>1120</number>
        <name>House number</name>
        <value>281</value>
        <description>281</description>
      </row>
      <row>
        <number>1160</number>
        <name>Postal</name>
        <value>2552AD</value>
        <description>2552AD</description>
      </row>
      <row>
        <number>1170</number>
        <name>Area</name>
        <value>Peru</value>
        <description>Peru</description>
      </row>
    </rows>";
        var file = XDocument.Parse(sampleXml);

        var query = from row in file.Descendants("row")
                    select new Person
                    {
                        Street = row.Element("number").Value,
                        HouseNumber = row.Element("name").Value,
                        Postal = row.Element("value").Value,
                        Area = row.Element("description").Value
                    };

        var list = query.ToList();