2014-10-20 80 views
0

我遇到問題。我想獲得元素的屬性。 我的問題的線索是,所有元素都有相同的名稱。獲取xml數據的屬性(wpf c#)

的XML數據如何看起來

<Sampels> 
    <Sampel Attribute1="a" Attribute2="b" Attribute3="3" Attribute4="d" /> 
    <Sampel Attribute1="asdf" Attribute2="b" Attribute3="3" Attribute4="name" /> 
    <Sampel Attribute1="" Attribute2="" Attribute3="66" Attribute4="attri" /> 
    <Sampel Attribute1="" Attribute2="b" Attribute3="" Attribute4="sampelname" /> 
</Sampels> 

我想知道正確的元素,從Attribute4規定獲得屬性。

+0

你應該看看看看http://stackoverflow.com/questions/13704752/deserialize-xml-to-object-using-dynamic。還有很多其他的解決方案,在Stackoverflow上,你應該先發布你的問題之前先搜索。 – TYY 2014-10-20 10:11:36

+0

他們在那裏使用childenotes,但我不這樣做,所以這兩個問題都不相同 – Archimedes 2014-10-20 10:15:16

+0

如果未拾取屬性,也可以修改以拾取屬性。我喜歡這個解決方案的原因是,它允許你做像foreach這樣的事情(var sample1 in sample)var attribute4 = sample1.Attribute4 – TYY 2014-10-20 10:23:07

回答

0

的XPath將做到這一點:

使用這些包括:

using System.Xml.Linq; 
using System.Xml.XPath; 

,找到你的屬性值(XML是你的XML作爲字符串):

string search = "sampelname"; 
    XDocument doc = XDocument.Parse(xml); 
    XElement el = doc.XPathSelectElement(string.Format("/Sampels/Sampel[@Attribute4='{0}']", search)); 
0

你可以嘗試像這個。

XElement xmlElement = XElement.Load("myFile.xml"); 


var attributes = (from e in xmlElement.Elements("Sample") 
        where e.Attribute("Attribute4").Value == "myAtribute4Value" 
        select new { 
         Attribute1 = e.Attribute("Attribute1").Value, 
         Attribute2 = e.Attribute("Attribute2").Value 
        }).FirstOrDefault(); 
0
static void Main(string[] main) 
{ 
     var samples = @"<Sampels> 
         <Sampel Attribute1='a' Attribute2='b' Attribute3='3' Attribute4='d' /> 
         <Sampel Attribute1='asdf' Attribute2='b' Attribute3='3' Attribute4='name' /> 
         <Sampel Attribute1='' Attribute2='' Attribute3='66' Attribute4='attri' /> 
         <Sampel Attribute1='' Attribute2='b' Attribute3='' Attribute4='sampelname' /> 
         </Sampels>"; 
     dynamic Sampels = DynamicXml.Parse(samples); 
     foreach(var sample1 in Sampels.Sampel) 
     { 
      Console.WriteLine(sample1.Attribute4); 
     } 

} 

// using http://stackoverflow.com/questions/13704752/deserialize-xml-to-object-using-dynamic 

public class DynamicXml : System.Dynamic.DynamicObject 
{ 
    XElement _root; 
    private DynamicXml(XElement root) 
    { 
     _root = root; 
    } 

    public static DynamicXml Parse(string xmlString) 
    { 
     return new DynamicXml(XDocument.Parse(xmlString).Root); 
    } 

    public static DynamicXml Load(string filename) 
    { 
     return new DynamicXml(XDocument.Load(filename).Root); 
    } 

    public override bool TryGetMember(System.Dynamic.GetMemberBinder binder, out object result) 
    { 
     result = null; 

     var att = _root.Attribute(binder.Name); 
     if (att != null) 
     { 
      result = att.Value; 
      return true; 
     } 

     var nodes = _root.Elements(binder.Name); 
     if (nodes.Count() > 1) 
     { 
      result = nodes.Select(n => new DynamicXml(n)).ToList(); 
      return true; 
     } 

     var node = _root.Element(binder.Name); 
     if (node != null) 
     { 
      if (node.HasElements) 
      { 
       result = new DynamicXml(node); 
      } 
      else 
      { 
       result = node.Value; 
      } 
      return true; 
     } 

     return true; 
    } 
}