【问题标题】:How do I get the xpath for this element?如何获取此元素的 xpath?
【发布时间】:2016-12-30 11:10:48
【问题描述】:

我需要返回dependentAssembly的codebase属性(即asmv1:assembly => dependency =>dependentAssembly(第一个)=> codebase属性)

这是 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xrml="urn:mpeg:mpeg21:2003:01-REL-R-NS" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1" xmlns:co.v2="urn:schemas-microsoft-com:clickonce.v2">
  <assemblyIdentity name="program.application" version="3.4.95.1045" publicKeyToken="98ecb8aa8cf73f16" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
  <description asmv2:publisher="publisher" asmv2:product="Magical Christmas Land" xmlns="urn:schemas-microsoft-com:asm.v1">Magical Christmas Land</description>
  <deployment install="true" minimumRequiredVersion="3.4.95.1045" co.v1:createDesktopShortcut="true">
    <subscription>
      <update>
        <beforeApplicationStartup />
      </update>
    </subscription>
   </deployment>
  <compatibleFrameworks xmlns="urn:schemas-microsoft-com:clickonce.v2">
   <framework targetVersion="4.5.2" profile="Client" supportedRuntime="4.0.30319" />
   <framework targetVersion="4.5.2" profile="Full" supportedRuntime="4.0.30319" />
  </compatibleFrameworks>
  <dependency>
    <dependentAssembly dependencyType="install" codebase="3.0.8\program.exe.manifest" size="214085">
      <assemblyIdentity name="program.exe" version="3.0.8" publicKeyToken="48ecb8aa8cf73f16" language="neutral" processorArchitecture="msil" type="win32" />
      <hash>
        <dsig:Transforms>
          <dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity" />
        </dsig:Transforms>
        <dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha256" />
        <dsig:DigestValue>rawr</dsig:DigestValue>
      </hash>
    </dependentAssembly>
  </dependency>

这是我尝试过的:

这可以工作并获取程序集元素,以及其中的 7 个子节点(依赖项是其中之一)。一旦我添加“/asmv1:assembly/dependency”,它就会失败并返回 null。

        var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");

        var node = xmlDoc.DocumentElement.SelectSingleNode(@"/asmv1:assembly", nsmgr);

这行得通,但它超级难看:

var childNode = node.ChildNodes
                            .OfType<XmlElement>()
                            .First(n => n.LocalName == "dependency")
                            .ChildNodes[0]
                            .Attributes["codebase"].InnerText;

【问题讨论】:

  • 你试过asmv1:assembly/asmv1:dependency吗?这可能行不通,但你永远不知道。
  • @G0dsquad 不,没用

标签: c# xml xpath


【解决方案1】:

dependency 继承默认命名空间,URI 为 urn:schemas-microsoft-com:asm.v2

var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1");
nsmgr.AddNamespace("asmv2", "urn:schemas-microsoft-com:asm.v2");

var node = xmlDoc.DocumentElement.SelectSingleNode(@"/asmv1:assembly/asmv2:dependency", nsmgr); 

【讨论】:

  • 谢谢!作为参考,最终的 xpath 是/asmv1:assembly/asmv2:dependency/asmv2:dependentAssembly[1]/@codebase
猜你喜欢
  • 2017-08-20
  • 2016-09-30
  • 1970-01-01
  • 2011-07-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多