我有一个类似于下面的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>我需要把每个CustomProperty放在每个node02下面。这是我的代码.
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值。我怎样才能得到CustomerPropery的node02 ?
非常感谢。
发布于 2011-06-21 11:10:59
我会用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";这里有一个XPath示例页,您可以看到什么是可能的。
发布于 2011-06-21 11:16:31
我不清楚你是如何想要这些数据的,只是这里没有足够的信息来处理。
相反,下面是一个示例,说明如何选择所有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(),
});https://stackoverflow.com/questions/6423723
复制相似问题