简体   繁体   English

通过Linq到XML获取父节点的属性值

[英]Get attribute value of parent node by Linq to XML

I have a problem parsing a XML file using Linq to XML. 我在使用Linq到XML解析XML文件时遇到问题。

My XML structure looks like: 我的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>

What I intend to do is to get the value of each Variable Name attribute. 我打算做的是获取每个“变量名”属性的值。 So for the elements that are directly under the node Module it works fine with 因此,对于直接位于节点模块下的元素,它可以与

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

The problem starts with the Variable Name attributes that are under the Task nodes because I also need the information about the Task Name. 问题开始于Task节点下的Variable Name属性,因为我还需要有关Task Name的信息。

So what I would like to get is the information about the Variable name plus the information about the Task Name that is the parent node of the variable element. 所以我想得到的是有关变量名称的信息以及有关作为变量元素的父节点的任务名称的信息。

Is there a way to get this done with Linq? 有没有办法用Linq做到这一点?

You can use parent property of XElement 您可以使用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")
});

The problem with your current code is since you are using Elements it is returning only the Variable which are direct child of root node. 当前代码的问题在于,由于您正在使用Elements,因此仅返回作为根节点直接子代的Variable Use Descedants instead. 请改用后代

This query will give you the expected output:- 该查询将为您提供预期的输出:

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

Check difference between Elements and Descendants . 检查元素和后代之间的差异

Descendants won't work in this case. 在这种情况下,后代将无法工作。 Try complete solution 尝试完整的解决方案

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM