c# - 获取 XElement 的名称

标签 c# xml windows-phone-7 xelement

我正在 Windows Phone 的应用程序中解析 xml。 我的 XML 看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<people>
    <person name="Kurt Cobain">
      <overall>
  
      </overall>
      <childhood>
  
      </childhood>
      <youth>
  
      </youth>
  
     <picture1>
  
      </picture1>
    </person>
  
</people>

我应该获取人员节点元素的名称(总体、童年、青年等),因为对于每个人员节点,它们都是不同的。 这是到目前为止我的代码,但查询结果为空:

XDocument loadedXml = XDocument.Load("people.xml");

                    var data = from query in loadedXml.Descendants("person")
                               where ((query.Attribute("name").Value as string).Equals("Kurt Cobain"))
                               select query.Elements();

                    
                     string test = "";
                     foreach (var item in data)
                     {
                         
                             test + = (item as XElement).Name.LocalName;
                         
                     }
                     MessageBox.Show(test);

最佳答案

从 linq 中删除 .Elements()

var data = from query in loadedXml.Descendants("person") 
           where ((query.Attribute("name").Value as string).Equals("Kurt Cobain")) 
           select query;

关于c# - 获取 XElement 的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11670115/

相关文章:

c# - 使用可执行路径打开文件的 nUnit 测试函数

c# - Ninject - 不同的解决方案配置

php - php 页面中包含 feed rss 的图像

android - 为什么 Android 用于企业应用程序?

c# - 如何在用户单击 Web 浏览器中的链接时进行拦截

c# - .Net 应用程序安装 2012 与数据库

C# Windows 窗体应用程序长按钮单击

php - 显示带有从 mysql 检索到的标记的谷歌地图

c# - 不使用扩展方法选择 XML 元素

windows-phone-7 - Windows Phone 打开另一个应用程序