我有以下XML代码段:
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" size="92160">
<hash>
<dsig:Transforms>
<dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
</dsig:Transforms>
<dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<dsig:DigestValue>CPxsdsbvZSAAkmARkxa8ychL2aLZRc=</dsig:DigestValue>
</hash>
</dependentAssembly>
</dependency>
如何使用LINQ或所有后代节点选择此节点:
<dsig:Transforms>
感谢。
答案 0 :(得分:1)
XDocument.Load("file.xml").Root.Descendants(XName.Get("dsig", "Transforms"));
答案 1 :(得分:0)
这样的事情应该有效:
XElement docElem = XElement.Load(pathToXml);
XNamespace ns = "http://www.w3.org/2000/09/xmldsig#";
// This assumes you know there will be exactly one "Transforms" element
XElement transforms = docElem.Descendants(ns + "Transforms").Single();
foreach (XElement transform in transforms.Elements()) {
// Do something with each Transform element
}
为此,必须加载完整的XML(带有名称空间前缀声明)。