0

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...

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

Help me!

Community
  • 1
  • 1
Victor Zhang
  • 52
  • 2
  • 7

1 Answers1

3

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

Hope that helps

Milind Thakkar
  • 980
  • 2
  • 13
  • 20
  • Awesome, this works fine. The special structure I mean is some xml node with a default namespace. And I get a deep understanding about this from another post http://stackoverflow.com/questions/4271689/xml-selectnodes-with-default-namespace-via-xmlnamespacemanager-not-working-as-ex – Victor Zhang Nov 27 '12 at 09:50