簡體   English   中英

獲取XML數據的屬性(WPF C#)

[英]Get Attributes of xml data (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指定的正確元素來獲取屬性。

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

您可以嘗試這樣。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM