问题描述
嗨.
我有看起来像
的xml文件
Hi.
I have xml files that look like
<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.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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:co.v1="urn:schemas-microsoft-com:clickonce.v1">
<asmv1:assemblyIdentity name="ClickOnceAppTest.exe" version="1.0.0.4" publicKeyToken="d1e81ecbb61bc9b1" language="neutral" processorArchitecture="x86" type="win32" />
<application />
<entryPoint>
<assemblyIdentity name="ClickOnceAppTest" version="1.0.0.1" language="neutral" processorArchitecture="x86" />
<commandLine file="ClickOnceAppTest.exe" parameters="" />
</entryPoint>
<trustInfo>
<security>
<applicationRequestMinimum>
等等.
我需要通过提取某些按条件(标记名,属性)匹配的标记来解析它.
到目前为止,我发现了两种按条件搜索的方法:
1.XDocument上的LINQ
2. XmlDocument的使用XPath的SelectNodes方法.
无论哪种情况,我都无法做到,因为我不太了解命名空间.当我尝试使用XDocument读取xml时,它会将一些asmv2:内容添加到内部xml节点中.例如,在源代码中,我有
and so on.
I need to parse it, by extracting certain tags that match by criteria (tagname, attributes).
So far I found 2 methods to search by criteria:
1.LINQ on XDocument
2.XmlDocument''s SelectNodes method using XPath.
In either case I can''t do that because I don''t quite understand namespacing. When I try to read xml with XDocument, it prepends some asmv2: stuff to inner xml nodes. For example, in source I have
<dependency>
<dependentAssembly dependencyType="install" allowDelayedBinding="true" codebase="ClickOnceAppTest.exe" size="26112">
<assemblyIdentity name="ClickOnceAppTest" version="1.0.0.1" language="neutral" processorArchitecture="x86" />
但是XDocument将asmv1前置为< asmv1:依赖关系xmlns:asmv1 =#unknown">因此xDoc.Descendants("dependency")不会产生任何结果.
使用XPath时,我会遇到没有结果的情况.我试图写
but XDocument prepends asmv1 yielding <asmv1:dependency xmlns:asmv1="#unknown"> and so xDoc.Descendants("dependency") yields nothing.
With XPath I get saame no results situation. I''ve tried to write
XmlNamespaceManager mngr = new XmlNamespaceManager(serverXml.NameTable);
string xPath="assembly/dependency/dependentAssembly/";
XmlNodeList emptySet = serverXml.SelectNodes(xPath, mngr);
那么,我该如何解决名称空间的问题呢?
P.S.抱歉,这种笨拙的样式-得知我不擅长某些基础知识,我的道德下降了.
So, how do I tackle the problem with namespaces?
P.S. Sorry for such a cumbersome style - my moral is down by learning that I am no good at some elementary stuff.
推荐答案
这篇关于.网. Xml命名空间,xml标记前缀和解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!