1

我确实有一个类似这样的 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
         });

    }

但我不知道如何使用电话号码读取节点,有人可以给出提示或一行代码我该怎么做!

4

3 回答 3

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>标签中,实际数字是内容,类型重试,类型数据是该节点的属性。

于 2013-08-16T19:46:04.897 回答
0

我认为您需要通过“联系人”元素内的“电话号码”元素循环。

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

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

于 2013-09-04T11:25:53.893 回答
0
_phonenumbers = subelement.Descendants("telephone_number")
                          .Select(x => 
                                 new List<string>() {
                                     (string)x.Element("number1"), 
                                     (string)x.Element("number2"),
                                     (string)x.Element("number3")
                           });
于 2013-08-16T19:45:52.783 回答