简体   繁体   English

如何基于特定标签名称使用XML和Linq获取元素的值

[英]How to get the value of elements using XML and Linq based off a specific tag name

Essentially, I have tried everything and for some reason I can't get the value of the elements in my XML based off the parameter I need it to meet. 本质上,我已经尝试了所有方法,由于某种原因,我无法根据需要满足的参数来获取XML中元素的值。 I feel like I'm close but I just don't know where I'm going wrong. 我觉得我已经接近了,但我不知道我要去哪里错了。 I'm trying to get the value of the elements and put them into a list to be used elsewhere. 我正在尝试获取元素的值并将它们放入列表中,以供其他地方使用。 Currently it doesn't put anything in the list. 目前,它没有列出任何内容。

I've tried XML Reader so now I'm giving Linq to XML a try but this won't work either. 我已经尝试过XML Reader,所以现在我可以尝试使用Linq到XML,但这也不起作用。

private List<string> outputPath = new List<string>();
var doc = XDocument.Load(Path.Combine(projectDirectory, "JobPaths.xml"));
foreach (var child in doc.Element("Jobs").Elements("Job").Where(x => x.Attribute("Name").ToString() == jobName).Elements())
{
    outputPath.Add(child.Name.ToString());
}

return outputPath;

Here's the XML: 这是XML:

<?xml version="1.0" encoding="utf-8" ?>
<Jobs>
  <Job Name="events_monitoring_c">
    <Path>\\stadb4412\</Path>
  </Job>
  <Job Name="events_monitoring_d">
    <Path>\\stadb4412\</Path>
    <Path>\\stadb1111\</Path>
    <Path>\\stadb2412\</Path>
  </Job>
</Jobs>

The jobName comes from the XML File, so I'm trying to get all the path elements based on the job name, regardless of how many there are. jobName来自XML文件,因此我试图基于作业名称获取所有路径元素,无论有多少。 I want to get all the paths in the list to be used elsewhere. 我想获取列表中的所有路径以在其他地方使用。

To find nodes of a specific type/tag from an XDocument or XElement you use .Descendants(name), then you have .Attribute(name) that returns an XAttribute. 要从XDocument或XElement查找特定类型/标记的节点,请使用.Descendants(name),然后使用.Attribute(name)返回XAttribute。 To get its value, you use .Value, not .ToString(). 要获取其值,请使用.Value,而不是.ToString()。

Your code gets the Job elements, but then it gets the children elements as an IEnumerable of nodes and for each of them adds the Name of the tags, which is always Path. 您的代码获取Job元素,但随后将其作为IEnumerable节点获取子元素,并为每个子元素添加标签的名称,该名称始终为Path。

What you are looking for is doc.Descendants("Job").Where(job=>job.Attribute("Name")?.Value==jobName).SelectMany(job=>job.Elements()).Select(elem=>elem.Value).ToList(); 您正在寻找的是doc.Descendants(“ Job”)。Where(job => job.Attribute(“ Name”)?. Value == jobName).SelectMany(job => job.Elements())。Select( elem => elem.Value).ToList();

I did it without compiling, so I may be wrong. 我没有编译就这样做了,所以我可能是错的。

You parse into a dictionary using Xml Linq : 您使用Xml Linq解析为字典:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, List<string>> dict = doc.Descendants("Job")
                .GroupBy(x => (string)x.Attribute("Name"), y => y)
                .ToDictionary(x => x.Key, y => y.Elements("Path").Select(z => (string)z).ToList());
        }
    }
}

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

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