我正在尝试阅读以下文件(部分呈现)
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd"
manifestVersion="1.0"
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"
xmlns="urn:schemas-microsoft-com:asm.v2"
xmlns:asmv1="urn:schemas-microsoft-com:asm.v1"
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">
<assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
我有
Dim manifestFile As String = webClient.DownloadString(location)
Dim xdoc As New Xml.XmlDocument()
xdoc.LoadXml(manifestFile)
我试过了
xdoc.SelectSingleNode("//asmv1:assemblyIdentity")
xdoc.SelectSingleNode("//assemblyIdentity")
Dim nsSys As Xml.XmlNamespaceManager = New Xml.XmlNamespaceManager(xdoc.NameTable)
nsSys.AddNamespace("def", "urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd")
xdoc.SelectSingleNode("//asmv1:assemblyIdentity",ns)
xdoc.SelectSingleNode("//assemblyIdentity",ns)
xdoc.SelectSingleNode("//def:assemblyIdentity",ns)
全部返回Nothing
选择<assemblyIdentity>
节点的正确途径是什么?
注意我不能使用LinqToXml
谢谢
答案 0 :(得分:2)
假设我关闭了标签 - 文件看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<asmv1:assembly xsi:schemaLocation="urn:schemas-microsoft-com:asm.v1 assembly.adaptive.xsd" manifestVersion="1.0" 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" xmlns="urn:schemas-microsoft-com:asm.v2" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" 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">
<assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
</asmv1:assembly>
以下适用于我:
Dim doc As New XmlDocument()
Dim nsmgr As New XmlNamespaceManager(doc.NameTable)
doc.Load("c:\temp\temp2.xml")
nsmgr.AddNamespace("asmv1", "urn:schemas-microsoft-com:asm.v1")
Dim node As XmlNode = doc.SelectSingleNode("//asmv1:assemblyIdentity", nsmgr)
MessageBox.Show(node.OuterXml)
并返回:
<assemblyIdentity name="program.application" version="4.0.0.27077" publicKeyToken="8f99fe9fddfae125e" language="neutral" processorArchitecture="msil" xmlns="urn:schemas-microsoft-com:asm.v1" />
<assemblyIdentity>
节点引用asmv1名称空间,因此需要将其添加到XMLNamespaceManager