2011-06-21 29 views
0

我有一个像下面这样的xml。Linq to 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> 

我需要得到每个NODE02下的每个CustomProperty的。这是我的代码。

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 : "" 
          }; 
} 

但在听到它的回报所有CustomPropery值。我如何获得Customer02的node02?

非常感谢。

+1

我接受您的评论。我通过投票接受了一些答案,但我通过舔正确的标记来确认它。和泰克斯的评论。它不会再发生。 – Charitha

+0

好东西。看到我的答案。 – spender

回答

3

我使用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 name="2">你可以改变路径,所有节点如下:

var path= 
    "/testing/node01/node02s/node02[@name=2]/CustomProperties/CustomProperty"; 

这里有一个page of XPath examples,所以你可以看到什么是可能的。

+0

Thanx的答复。但仍然无法单独获取CustomProperies。我需要分别获取Node02名称1下的CustomProperties和Node02名称2下的CustomProperties。 – Charitha

+0

XPath支持这一点。看我的编辑。 – spender

+0

是的。它的工作现在。这就是我一直在寻找的东西。非常感谢。 – Charitha

1

目前还不清楚您想要数据的方式,这里没有足够的信息来处理。

相反,下面是一个示例,说明如何选择所有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(),     
    });