簡體   English   中英

通過Linq到XML獲取父節點的屬性值

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

我在使用Linq到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"));

問題開始於Task節點下的Variable Name屬性,因為我還需要有關Task Name的信息。

所以我想得到的是有關變量名稱的信息以及有關作為變量元素的父節點的任務名稱的信息。

有沒有辦法用Linq做到這一點?

您可以使用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")
});

當前代碼的問題在於,由於您正在使用Elements,因此僅返回作為根節點直接子代的Variable 請改用后代

該查詢將為您提供預期的輸出:

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

檢查元素和后代之間的差異

在這種情況下,后代將無法工作。 嘗試完整的解決方案

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