XML使用LINQ解析节点和子节点

时间:2013-08-16 19:37:48

标签: c# xml linq linq-to-xml silverlight-5.0

我确实有类似这样的XML

<?xml version="1.0" encoding="UTF-8"?>
<e_schema>
    <schema_name value="shema1">
        <contact>
            <id>1</id>
            <firstName>firstname1</firstName>
            <lastName>lastname1</lastName>
            <department>IT</department>
            <emailAddress>lastname1@mydomain.com</emailAddress>
            <lineManagerId>22331470</lineManagerId>
            <telephone_number>
                <number1>0000000000</number1>
                <number2>1111111111</number2>
                <number3>2222222222</number3>
                <retries1>2</retries1>
                <retries2>1</retries2>
                <retries3>2</retries3>
                <numberType1>Mobile</numberType1>
                <numberType2>Fixnet</numberType2>
                <numberType3>Fixnet</numberType3>
            </telephone_number>
        </contact>
        <contact>
            <id>2</id>
            <firstName>firstname2</firstName>
            <lastName>lastname2</lastName>
            <department>SUPPORT</department>
            <emailAddress>lastname2@mydomain.com</emailAddress>
            <lineManagerId>22331470</lineManagerId>
            <telephone_number>
                <number1>3333333333</number1>
                <number2>4444444444</number2>
                <number3>5555555555</number3>
                <retries1>2</retries1>
                <retries2>1</retries2>
                <retries3>2</retries3>
                <numberType1>Mobile</numberType1>
                <numberType2>Fixnet</numberType2>
                <numberType3>Fixnet</numberType3>
            </telephone_number>
        </contact>
    </schema_name>
</e_schema>

现在用这段代码我读取每个联系节点并将它们添加到列表

    var xmlcontacts = xmlloaded.Descendants("schema_name").Where(node => (string)node.Attribute("value") == comboSchema.SelectedValue.ToString());

    foreach (XElement subelement in xmlcontacts.Descendants("contact")) //element is variable
    {

        contact.Add(new Contact()
         {
             id = subelement.Element("id").Value,
             firstName = subelement.Element("firstName").Value,
             lastName = subelement.Element("lastName").Value,
             department = subelement.Element("department").Value,
             emailAddress = subelement.Element("emailAddress").Value,
             lineManagerId = subelement.Element("lineManagerId").Value,
             //_phonenumbers = phones
         });

    }

但是我没有任何想法如何用telephone_number读取节点 有人可以提供一些提示或一行代码,我可以做到这一点!

3 个答案:

答案 0 :(得分:1)

我认为_phonenumbers是某种集合,例如IEnumerable<PhoneInfo>

_phoneNumbers = subelement.Element("telephone_number").Elements()
                .Where(e => e.Name.LocalName.StartsWith("number").Select(e => 
                new PhoneInfo
                {
                  Number = e.Value,
                  Retries = subelement.Element("telephone_Number").Element(
                  "retries" + e.Name.LocalName.SubString(5)).Value,
                  NumberType = subelement.Element("telephone_Number").Element(
                  "numbertype" + e.Name.LocalName.SubString(5)).Value
                })

代码使用linq表达式为每个数字创建一个PhoneInfo实例,并查找相应的重试次数和数字类型。

作为一个注释:xml结构非常糟糕,将<number>标记中的所有数字与实际数字作为内容和类型重试以及类型数据作为该节点的属性会更好。

答案 1 :(得分:0)

_phonenumbers = subelement.Descendants("telephone_number")
                          .Select(x => 
                                 new List<string>() {
                                     (string)x.Element("number1"), 
                                     (string)x.Element("number2"),
                                     (string)x.Element("number3")
                           });

答案 2 :(得分:0)

我认为你需要通过“contact”元素中的“telephone_number”元素循环。

以下是您可以尝试的代码:

var xmlcontacts = xmlloaded.Descendants("schema_name").Where(node => (string)node.Attribute("value") == comboSchema.SelectedValue.ToString());

    foreach (XElement subelement in xmlcontacts.Descendants("contact")) //element is variable
    {

        contact.Add(new Contact()
        {
            id = subelement.Element("id").Value,
            firstName = subelement.Element("firstName").Value,
            lastName = subelement.Element("lastName").Value,
            department = subelement.Element("department").Value,
            emailAddress = subelement.Element("emailAddress").Value,
            lineManagerId = subelement.Element("lineManagerId").Value,
            //_phonenumbers = phones
        });
        foreach (XElement phoneElement in subelement.Descendants("telephone_number"))
        {
            //add telephone_number details in list here
        }

    }

我刚刚在“联系”循环中添加了一个foreach