如何使用C#选择具有XML命名空间的XML节点 [英] How to Select XML Nodes with XML Namespaces with C#

查看:104
本文介绍了如何使用C#选择具有XML命名空间的XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用C#分析具有特殊名称空间的XML文档,并且从

I have to analyze a XML doc with special namespace using C#, and I get some idea from this post. But my code fail to get the expected XML node, because the XML structure is very special...

XML的根节点中有一个命名空间

There is a namespace in root node in XML

<MDOC xmlns="urn:schemas-microsoft-com/PSS/PSS_Survey01">

这是获取此根节点的代码

Here is my code to get this root node

        XmlDocument doc = new XmlDocument();
        doc.Load(path);

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
        nsmgr.AddNamespace("urn", "schemas-microsoft-com/PSS/PSS_Survey01");

        XmlNode root = doc.SelectSingleNode("MDOC", nsmgr);

救救我!

推荐答案

我不确定您的XML结构有什么特别之处.

I am not sure what is special about your XML structure.

我写的代码几乎没有什么

I would write the code little differently

string xmlNamespace = String.Empty;
XmlNamespaceManager nsmgr;
XmlNodeList nodeInfo = FABRequestXML.GetElementsByTagName("RootNodeName");
xmlNamespace = Convert.ToString(nodeInfo[0].Attributes["xmlns"].Value);
nsmgr = new XmlNamespaceManager(MyXml.NameTable);
nsmgr.AddNamespace("AB", xmlNamespace);

XmlNode myNode = MyXml.DocumentElement.SelectSingleNode("AB:NodeName", nsmgr);

希望有帮助

这篇关于如何使用C#选择具有XML命名空间的XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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