2015-01-02 72 views
0

我想獲得匹配值的屬性值的值。在這裏我的XML文件如何獲得匹配值的屬性值?

<Setup> 
    <Include Type="Product"> 
    <Value uomid="8078">468922</Value> 
    <Value uomid="8078">468908</Value> 
    </Include> 
</Setup> 

所以,我想採取與匹配值的uomid基地。而在代碼隱藏防爆的過程:我把我的參數值是:468922所以我的結果,我想要得到的是468922 8078不是468908.由於

+0

[*你嘗試過什麼?*](http://www.whathaveyoutried.com) –

+0

兩個屬性值是一樣的。這是一個錯字錯誤嗎? –

+0

現在,我可以獲取參數值,但我不知道獲取基於參數值的元素值的方法。 –

回答

1

8087嘗試下面的代碼:

SearchValue是價值你想要搜索。在你的情況下,其 「468922」

public string ReturnAttribute(string SearchValue) 
     { 
      XDocument xdoc = XDocument.Load(@"C:\Tmp\test.xml"); 
      string ReturnValue = String.Empty; 
      foreach (var item in xdoc.Descendants("Value")) 
      { 
       if (item.Value == SearchValue) 
       { 
        ReturnValue=item.FirstAttribute.Value; 

       } 
      } 
      return ReturnValue; 
     } 
+0

這與我使用XmlDocument時的方式相同? –

+0

首先將XMLDocument轉換爲XDocument,然後嘗試該方法。 –

+0

轉換http://stackoverflow.com/questions/1508572/converting-xdocument-to-xmldocument-and-vice-versa –