2013-08-16 163 views
1

我有類似這樣的XML解析節點和子節點與LINQ

<?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>[email protected]</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>[email protected]</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> 

一個XML現在這段代碼我讀每一個接觸點,並將它們添加到列表

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

    } 

但我沒有任何想法如何我可以讀取節點與電話號碼 有人可以給我一個提示或一行代碼我可以做到這一點!

+0

'subelement.Element( 「TELEPHONE_NUMBER」)後裔()'會。將「telephone_number」標籤內的所有標籤帶給您。你可以使用他們的'Value'屬性。 – AgentFire

+0

感謝您的提示。 – user2367263

回答

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>標籤中的所有數字都作爲內容和類型重試以及類型數據作爲該節點的屬性,會更好。

+0

謝謝,我試過你的例子,但我堅持與「e.Name.StartsWith」我沒有「StartsWith」! – user2367263

+0

對不起,我以爲'Name'屬性是一個字符串,事實並非如此。你必須插入'LocalName'來獲取字符串。看我的編輯。 –

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

我認爲你需要在「contact」元素中循環「telephone_number」元素。

下面是代碼,你可以嘗試一下:我剛纔添加里面的 「接觸」 多一個的foreach

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 
     } 

    }