XmlDocument.SelectSingleNode和XML命名空间的问题 [英] XmlDocument.SelectSingleNode and xmlNamespace issue

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

问题描述

我加载字符串到包含以下结构的XML文件:

I'm loading a string to an XML document that contains the following structure :

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">                  
  <ItemGroup>
    <Compile Include="clsWorker.cs" />        
  </ItemGroup>      
</Project>

然后是IM加载到所有的XmlDocument:

then im loading all into xmldocument :

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(Xml);

然后发生以下问题:

then the following problem occurs :

XmlNode Node = xmldoc.SelectSingleNode("//Compile"); // return null

当我删除的xmlns从根元素(项目),其做工精致属性,
我怎样才能提高我的SelectSingleNode返回相关的元素?

when i remove the xmlns attribute from the root element(Project) its working fine, how can i improve my SelectSingleNode to return the relevant element ?

推荐答案

您应该使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.xml.xmlnamespacemanager.aspx\">XmlNamespaceManager在调用的SelectSingleNode()

XmlNamespaceManager ns = new XmlNamespaceManager(xmldoc.NameTable);
ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
XmlNode node = xmldoc.SelectSingleNode("//msbld:Compile", ns);

这篇关于XmlDocument.SelectSingleNode和XML命名空间的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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