【问题标题】:Get attribute value of parent node by Linq to XMLLinq to XML获取父节点的属性值
【发布时间】:2016-06-10 14:49:17
【问题描述】:

我在使用 Linq to XML 解析 XML 文件时遇到问题。

我的 XML 结构如下所示:

<Module>
  <Variable Name="Variable1" Value="True" />
  <Variable Name="Variable2" Value="True" />
  <Variable Name="Variable3" Value="True" />
  </Task>
  <Task Name="Task1">
    <Variable Name="Task1Variable1" Value ="True" />
    <Variable Name=" Task1Variable2" Value ="True" />
  </Task>
  <Task Name="Task2">
    <Variable Name="Task2Variable1" Value ="True" />
    <Variable Name=" Task2Variable2" Value ="True" />
  </Task>
</Module>

我打算做的是获取每个变量名称属性的值。 因此,对于直接在节点模块下的元素,它可以正常工作

var variables = (from cfg in _xElements.Descendants("Module").Elements("Variable")
                                       select cfg.Attribute("Name"));

问题始于任务节点下的变量名称属性,因为我还需要有关任务名称的信息。

所以我想得到的是关于变量名称的信息以及关于作为变量元素的父节点的任务名称的信息。

有没有办法用 Linq 完成这项工作?

【问题讨论】:

  • _xElements.Descendants("Variable")???
  • @Viru:你的意思是什么?
  • @Viru:THX,我的问题是我不知道我可以以这种方式使用“select new”。我一直认为我必须实例化任何类型的新对象。非常感谢。
  • @ck8vi 是的,这叫匿名类型...

标签: c# xml linq xml-parsing linq-to-xml


【解决方案1】:

您可以使用 XElement 的父属性

var variables = (from cfg in _xElements.Descendants("Variable")
                                       select new
{
  TaskName = cfg.Parent.Name == "Task"? cfg.Parent.Attribute("Name"):null,   
  VariableAttribute = cfg.Attribute("Name")
});

【讨论】:

    【解决方案2】:

    您当前代码的问题在于,由于您使用的是Elements,因此它仅返回作为根节点的直接子节点的Variable。请改用Descedants

    这个查询会给你预期的输出:-

     var variables = (from cfg in _xElements.Descendants("Variable")
                      select cfg.Attribute("Name"));
    

    Check difference between Elements and Descendants.

    【讨论】:

      【解决方案3】:

      在这种情况下,后代将无法工作。尝试完整的解决方案

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      using System.Xml;
      using System.Xml.Linq;
      
      namespace ConsoleApplication1
      {
          class Program
          {
              static void Main(string[] args)
              {
                  string xml = 
                      "<Module>" +
                        "<Variable Name=\"Variable1\" Value=\"True\" />" +
                        "<Variable Name=\"Variable2\" Value=\"True\" />" +
                        "<Variable Name=\"Variable3\" Value=\"True\" />" +
                        "<Task Name=\"Task1\">" +
                          "<Variable Name=\"Task1Variable1\" Value =\"True\" />" +
                          "<Variable Name=\"Task1Variable2\" Value =\"True\" />" +
                        "</Task>" +
                        "<Task Name=\"Task2\">" +
                          "<Variable Name=\"Task2Variable1\" Value =\"True\" />" +
                          "<Variable Name=\"Task2Variable2\" Value =\"True\" />" +
                        "</Task>" +
                      "</Module>";
      
                  XDocument doc = XDocument.Parse(xml);
      
                  var results = doc.Elements("Module").Select(m => new {
                      variables = m.Elements("Variable").Select(n => new {
                         name = (string)n.Attribute("Name"),
                         value = (string)n.Attribute("Value")
                      }).ToList(),
                      tasks = m.Elements("Task").Select(o => new {
                          name = (string)o.Attribute("Name"),
                          variables = o.Elements("Variable").Select(p => new {
                              name = (string)p.Attribute("Name"),
                              value = (string)p.Attribute("Value")
                          }).ToList()
                      }).ToList()
                  }).FirstOrDefault();
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-01
        • 2012-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多