简体   繁体   English

如何使用C#选择具有XML命名空间的XML节点

[英]How to Select XML Nodes with XML Namespaces with C#

I have to analyze a XML doc with special namespace using C#, and I get some idea from this post . 我必须使用C#分析具有特殊名称空间的XML文档,并且从这篇文章中可以了解到一些想法。 But my code fail to get the expected XML node, because the XML structure is very special... 但是我的代码无法获得预期的XML节点,因为XML结构非常特殊...

There is a namespace in root node in XML 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);

Help me! 帮我!

I am not sure what is special about your XML structure. 我不确定您的XML结构有什么特别之处。

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);

Hope that helps 希望能有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM