C# Linq到XML(在一个节点下获取特定值)

C# Linq到XML(在一个节点下获取特定值),c#,linq-to-xml,C#,Linq To Xml,我有一个类似下面的xml。 <testing> <node01 name="node01Name"> <node02s> <node02 name="1"> <CustomProperties> <CustomProperty Name="ASCII File Name" Value="index.txt" FilterID="0" /> &

我有一个类似下面的xml。

  <testing>
  <node01 name="node01Name">
    <node02s>
      <node02 name="1">
        <CustomProperties>
          <CustomProperty Name="ASCII File Name" Value="index.txt" FilterID="0" />
          <CustomProperty Name="ASCII Folder" Value="\\abc\cdf\aaaa" FilterID="0" />
          <CustomProperty Name="Delimiter" Value="CommaQuote" FilterID="0" />
          <CustomProperty Name="Duplicate Handling" Value="Replace if Dup" FilterID="0" />
          <CustomProperty Name="EMAILATTDOC" Value="1" FilterID="0" />
          <CustomProperty Name="EMAILATTINDEX" Value="0" FilterID="0" />
          <CustomProperty Name="EMAILOUTBODY" FilterID="0" />
          <CustomProperty Name="EMAILOUTCC" FilterID="0" />
        </CustomProperties>
      </node02>
      <node02 name="2">
        <CustomProperties>
          <CustomProperty Name="ASCII File Name" Value="index.txt" FilterID="0" />
          <CustomProperty Name="ASCII Folder" Value="\\abc\cdf\aaaa" FilterID="0" />
          <CustomProperty Name="Delimiter" Value="CommaQuote" FilterID="0" />
          <CustomProperty Name="Duplicate Handling" Value="Replace if Dup" FilterID="0" />
          <CustomProperty Name="EMAILATTDOC" Value="1" FilterID="0" />
          <CustomProperty Name="EMAILATTINDEX" Value="0" FilterID="0" />
          <CustomProperty Name="EMAILOUTBODY" FilterID="0" />
          <CustomProperty Name="EMAILOUTCC" FilterID="0" />
        </CustomProperties>
      </node02>
    </node02s>    
  </node01>  
</testing>
XDocument configparentXML = XDocument.Load("admin.xml");
string node = "node02";

var batchClasses = from batchClasse in configparentXML.Descendants(node)
                   select new ReadingXmlWithLinq
                   {
                       BatchClassName = batchClasse.Attribute("Name") != null ? batchClasse.Attribute("Name").Value : "",
                   };

foreach (var lv0 in batchClasses)
{
    node = "CustomProperty";
    var CustomProperties = from CustomProperty in configparentXML.Descendants(node)                                       
                           select new ReadingXmlWithLinq
                           {
                               CustomPropertyName = documentClasse.Attribute("Name") != null ? documentClasse.Attribute("Name").Value : ""
                           };
}
但在听到它返回所有CustomProperty值时。如何获取节点的CustomerProperty 02?


非常感谢。

我想用XPath来解决这个问题:

var xmlString = @"<testing>...</testing>";
var doc = XDocument.Parse(xmlString);
var nav = doc.CreateNavigator();
var path = "/testing/node01/node02s/node02/CustomProperties/CustomProperty";
var nodeIterator = nav.Select(path);
var nodes =
    nodeIterator
        .Cast<XPathNavigator>()
        .Select(n=>XElement.Parse(n.OuterXml));

下面是一个示例,您可以看到可能的情况。

我将使用XPath实现这一点:

var xmlString = @"<testing>...</testing>";
var doc = XDocument.Parse(xmlString);
var nav = doc.CreateNavigator();
var path = "/testing/node01/node02s/node02/CustomProperties/CustomProperty";
var nodeIterator = nav.Select(path);
var nodes =
    nodeIterator
        .Cast<XPathNavigator>()
        .Select(n=>XElement.Parse(n.OuterXml));

下面是一个示例,您可以看到可能的情况。

我不清楚您想要的数据是什么,这里没有足够的信息可供使用

相反,这里有一个示例,说明如何选择所有
node02
元素并获取它们的名称及其
CustomProperty
的名称。这将选择所有的
node02
,以及它们的
CustomProperty
,就像您的代码一样。实际上,您没有过滤任何内容,也没有指定所需的节点或属性。这让人搞不清你到底想要什么。因此,以下是一个示例,展示了如何做您似乎想做的事情:

XDocument doc = ...;
var batchClasses = doc.Descendants("node02")
    .Select(n => new
    {
        BatchClassName = (string)n.Attribute("name") ?? "",
        CustomPropertyNames = n.Descendants("CustomProperty")
            .Select(cp => (string)cp.Attribute("Name") ?? "")
            .ToList(),
        // Here's an example to select "EMAIL" custom property names
        EmailPropertyNames = n.Descendants("CustomProperty")
            .Select(cp => (string)cp.Attribute("Name") ?? "") // select the names...
            .Where(s => s.StartsWith("EMAIL"))                // that start with "EMAIL"
            .ToList(),                
    });

我不清楚你想要什么样的数据,只是这里没有足够的信息来处理

相反,这里有一个示例,说明如何选择所有
node02
元素并获取它们的名称及其
CustomProperty
的名称。这将选择所有的
node02
,以及它们的
CustomProperty
,就像您的代码一样。实际上,您没有过滤任何内容,也没有指定所需的节点或属性。这让人搞不清你到底想要什么。因此,以下是一个示例,展示了如何做您似乎想做的事情:

XDocument doc = ...;
var batchClasses = doc.Descendants("node02")
    .Select(n => new
    {
        BatchClassName = (string)n.Attribute("name") ?? "",
        CustomPropertyNames = n.Descendants("CustomProperty")
            .Select(cp => (string)cp.Attribute("Name") ?? "")
            .ToList(),
        // Here's an example to select "EMAIL" custom property names
        EmailPropertyNames = n.Descendants("CustomProperty")
            .Select(cp => (string)cp.Attribute("Name") ?? "") // select the names...
            .Where(s => s.StartsWith("EMAIL"))                // that start with "EMAIL"
            .ToList(),                
    });

我接受你的意见。有些答案我已经投票接受了。但我不能通过舔正确的标记来确认。对此我很抱歉。和thax的评论。这种事再也不会发生了。我接受你的意见。有些答案我已经投票接受了。但我不能通过舔正确的标记来确认。对此我很抱歉。和thax的评论。这不会再发生了。谢谢你的回复。但我仍然无法单独获得定制。我需要分别获取Node02 name 1下的CustomProperties和Node02 name 2下的CustomProperties。是。现在开始工作了。这就是我要找的。谢谢你的回复。但我仍然无法单独获得定制。我需要分别获取Node02 name 1下的CustomProperties和Node02 name 2下的CustomProperties。是。现在开始工作了。这就是我要找的。很多。