我有一个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值。如何获得node02的CustomerPropery?
非常感谢。
答案 0 :(得分: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,所以你可以看到什么是可能的。
答案 1 :(得分:1)
我不清楚你想要的数据,这里没有足够的信息可供使用。
相反,这是一个示例,说明如何选择所有node02
元素并获取其名称和CustomProperty
的名称。这会像您的代码一样选择 ALL node02
和 ALL 他们的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(),
});