【问题标题】:parsing XML content - C#解析 XML 内容 - C#
【发布时间】:2013-08-30 00:26:28
【问题描述】:

我已经很久没有使用 XML 并且需要从 XML 响应中提取有用的信息。如果有 2 个标签相同但名称不同,例如

    <lst name = "stack">
       <str>Ola</str>
       <lst name = "overflow">
          <str>Hello</str>
       </lst>
     </lst>

如何提取带有 name="overflow" 的标签的内容?

【问题讨论】:

  • 到目前为止你写了什么代码?这个问题与 XPath 查询或 C# 中的 XML 对象有关吗?
  • 你确定&lt;lst&gt; 是嵌套的吗?
  • 是的,C# 中的 XML 对象

标签: c# asp.net xml


【解决方案1】:

您可以使用 LINQ To XML:

var result = XDocument.Parse(xml)
                .Descendants("lst")
                .Where(e => (string) e.Attribute("name") == "overflow")
                .Descendants("str")
                .Select(x => x.Value)
                .FirstOrDefault();

【讨论】:

  • 您可以使用e.Attribute("name").Value。我认为它比string的演员更好
  • @SamLeach: 是的,更好,但是当 xml 上没有属性 name 时会出现异常
  • 啊,好吧。使用演员表,它将是一个空字符串?
  • @SamLeach:它返回null
【解决方案2】:

试试这个开始:

XPathDocument docNav = new XPathDocument(pathName);
XPathNavigator nav = docNav.CreateNavigator();
XmlNamespaceManager ns = new XmlNamespaceManager(nav.NameTable);

string val =  nav.SelectSingleNode(@"/lst/lst[@name='overflow']/str")

这些是用于简单 XPath 导航和 .NET XML 解析的好资源:

http://www.w3schools.com/xpath/

http://www.codeproject.com/Articles/52079/Using-XPathNavigator-in-C

【讨论】:

  • 你不需要在那个名字上加上@吗=?
  • +1 提供简洁的 xpath 解决方案,而不是复杂的 LINQ 替代方案
【解决方案3】:

您可以使用System.Xml.Linq 命名空间:

var xDoc = XDocument.Parse(xml);
var result = xDoc.Descendants()
    .Where(d => 
        d.Name == "lst" && 
        d.Attributes("name").FirstOrDefault()!=null &&
        d.Attributes("name").FirstOrDefault().Value == "overflow")
    .FirstOrDefault();

【讨论】:

    【解决方案4】:

    用户 Linq 到 xml

    var xmlFile = XDocument.Load(someFile);
    var query = from item in xmlFile.Descendants("childobject")
                where !String.IsNullOrEmpty(item.Attribute("using")
                select new 
                {
                    AttributeValue = item.Attribute("using").Value
                };
    

    【讨论】:

      【解决方案5】:

      你可以用 LINQ to XML 做到这一点:

      var doc = XDocument.Load("YourXMLPath.xml");
      var content = doc
      .Element("lst")
      .Elements("lst")
      .Where(e=>((string) e.Attribute("name") ?? "")=="overflow")
      .Select(e=>e.Element("str").InnerText())
      .FirstOrDefault();
      

      【讨论】:

        【解决方案6】:

        System.Xml.Linq 命名空间中的 LINQ to XML。

        const string xml = @"<lst name = ""stack""><str>Ola</str><lst name = ""overflow""><str>Hello</str></lst></lst>";
        
        XDocument doc = XDocument.Parse(xml);
        
        IEnumerable<XElement> overflow = doc.Root.Elements("lst").Where(x => (string) x.Attribute("name") == "overflow");
        
        XElement firstOverflow = overflow.FirstOrDefault();
        
        string value = firstOverflow.Descendants("str").FirstOrDefault(x => x.Value);
        

        【讨论】:

        • 如果lst 元素没有name 属性,这将引发异常。
        猜你喜欢
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2021-07-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-07
        • 1970-01-01
        相关资源
        最近更新 更多