简体   繁体   中英

XML parse nodes and subnodes with LINQ

I do have a XML similar like this

<?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>

now with this piece of code I read each of the contact node and add them to a list

    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
         });

    }

but I do not have any Idea how I can read the node with the telephone_number can someone give a hint or a line of code how I can do that!

I assume that _phonenumbers is some kind of collection, eg an 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
                })

The code uses a linq expression to create a PhoneInfo instance for each number, and it looks up the corresponding retries and number type.

As a note: The xml structure is quite bad, it would be much better to have all the numbers in <number> tags with the actual number being the content and type retries and type data being attributes of that node.

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

I think you need to loop thru "telephone_number" element inside "contact" element.

Below is the code you can try:

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
        }

    }

I have just added one more foreach inside the "contact" loop

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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