2013-06-03 71 views
0

IHAVE一個大的XML文件,我想給父母子元素的值來獲得子元素的值,我在XML文件中的新請任何幫助,這裏是我的xml:查找特定的XML元素containt

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<masterController> <uuid>XXXXXXXXXXXXXXXXXXXXXXXXX</uuid> 
<channels> 
    <channel> 
     <nodeGroups> 
     <nodeGroup> 
      <analogNode> 
      <typeCode>8</typeCode> 
      <id>1</id> 
      <sdos> 
       <sdo> 
       <description>Host ID</description> 
       <compareLevel>Ignore</compareLevel> 
       <datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance" 
xsi:type="intField"> 
        <description>Host ID</description> 
        <compareLevel>Ignore</compareLevel> 
        <offset>2</offset> 
        <size>1</size> 
        <readonly>true</readonly> 
        <isMappedToPdo>false</isMappedToPdo> 
        <ownerNodeSerial>12102904</ownerNodeSerial> 
        <ownerSdoIndex>3</ownerSdoIndex> 
        <data xsi:type="intData"> 
        <value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">2</value> 
        <unit></unit> 
        <min>1</min> 
        <max>15</max> 
        </data> 
        <intValue>2</intValue> 
       </datafield> 
       <index>3</index> 
       <totalbytes>3</totalbytes> 
       </sdo> 
       <sdo> 
       <description>Host ID</description> 
       <compareLevel>Ignore</compareLevel> 
       <datafield xmlns:xsi="http://www.XXXXX.XXXX/XXXXX/XMLSchema-instance" 
xsi:type="intField"> 
        <description>Host ID</description> 
        <compareLevel>Ignore</compareLevel> 
        <offset>2</offset> 
        <size>1</size> 
        <readonly>true</readonly> 
        <isMappedToPdo>false</isMappedToPdo> 
        <ownerNodeSerial>12102905</ownerNodeSerial> 
        <ownerSdoIndex>4</ownerSdoIndex> 
        <data xsi:type="intData"> 
        <value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value> 
        <unit></unit> 
        <min>1</min> 
        <max>15</max> 
        </data> 
        <intValue>2</intValue> 
       </datafield> 
       <index>3</index> 
       <totalbytes>3</totalbytes> 
       </sdo> 
      </sdos> 
      </analogNode> 
     </nodeGroup> 
     </nodeGroups> 
    </channel> </channels> </masterController> 

我正在嘗試這一點,但我不是歌廳什麼:

XElement root = XElement.Load(Server.MapPath("sample.xml")); 

       IEnumerable<XElement> masterco = from el in root.Elements("sdo") where (from add in el.Elements("datafield") 

        where 
         (string)add.Element("ownerNodeSerial") == TextBox1.Text && 

         (string)add.Element("ownerSdoIndex") == TextBox1.Text 

        select add) 

        .Any() 
        select el; 
       foreach (XElement el in masterco) 
       { 

       TextBox3.Text = (string)el.Element("value"); 
       } 

我想要得到這樣的:

<value xmlns:xs="http://www.XX.CC/2XXX/XMLSchema" xsi:type="xs:int">16</value> 

,並能夠UPD吃了它。

回答

1

有在查詢一個重大的錯誤:

您正在使用Elementsroot,但你正在尋找的標籤sdo這是不是根標籤的直接子。您必須改用Descendants

此外,我想你想有一個OR而不是AND關於文本TextBox1

修復:

var masterco = from el in root.Descendants("sdo") 
       where (from add in el.Elements("datafield") 
         where 
          (string)add.Element("ownerNodeSerial") == TextBox1.Text || 
          (string)add.Element("ownerSdoIndex") == TextBox1.Text 
         select add).Any() 
       select el; 

真正得到你想要的值,你應該使用不同的查詢。根本不需要選擇sdo標籤。

var value = root.Descendants("datafield") 
       .Where(x => (string)x.Element("ownerNodeSerial") == TextBox1.Text || 
          (string)x.Element("ownerSdoIndex") == TextBox1.Text) 
       .Select(x => (string)x.Element("data").Element("value")) 
       .Single(); 

TextBox3.Text = value; 

你可以看到,我假設整個XML文檔只有一個匹配的datafield/data/value條目存在英寸我從你更新你的文本框的方式獲取信息。如果有多個標籤,這將是沒有意義的 - 值將在文本框中覆蓋彼此。

+0

我試過這個,但得到錯誤說,序列不包含元素爲什麼是這樣的? PLZ幫助我。 – Clement

+0

@Clement:很可能是因爲沒有任何節點,其中'ownerNodeSerial'和'ownerSdoIndex'彼此相同,另外與'TextBox1'中的文本相同。也許你的意思是說其中的一個應該等於文本框中的文本?在這種情況下,您需要使用'||'而不是'&&'。 –

+0

@Clement:另外,請參閱更新。你的情況沒有命名空間問題。我錯了,並從我的答案中刪除了它。 –