解析XML内容 - C# [英] parsing XML content - C#

查看:148
本文介绍了解析XML内容 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我没有使用XML很长,需要提取XML响应的有用信息。如果有2个标记,是相同的,但有一个不同的名称例如

I have not used XML for very long and need to extract the useful information from an XML response. If there are 2 tags that are the same but have a different name e.g

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

我将如何提取标签的内容与NAME =溢出?

How would I extract the contents of the tag with name="overflow"?

推荐答案

您可以使用LINQ到XML:

You can use LINQ To XML:

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

这篇关于解析XML内容 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆