.网. Xml命名空间,xml标记前缀和解析 [英] .NET. Xml namespaces, xml tags prefixes and parsing

查看:133
本文介绍了.网. Xml命名空间,xml标记前缀和解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨.
我有看起来像
的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.

推荐答案

我认为当使用XmlReader而不是XmlDocument.为了处理名称空间,您将需要使用XmlNamespaceManager进行更多操作.您可以删除示例的名称空间.看看如何控制这些名称空间:
http://msdn.microsoft.com/en-us/library/d6730bwt.aspx [ ^ ]

您还可以检索每个元素的名称空间:
http://msdn.microsoft.com/en-us/library/system.xml. xmlreader.namespaceuri [ ^ ]

祝你好运!
I think it would get easier and more efficient when using a XmlReader instead of XmlDocument. For handling the namespaces you would need to do more with the XmlNamespaceManager. You could remove the namespaces for examples. Have a look how to control those namespaces:
http://msdn.microsoft.com/en-us/library/d6730bwt.aspx[^]

You can also retrieve the namespace for each element:
http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.namespaceuri[^]

Good luck!


这篇关于.网. Xml命名空间,xml标记前缀和解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆