troubleshooting Question

XmlNode.SelectNodes() problem with namespace

Avatar of steelheart38
steelheart38Flag for Philippines asked on
Web Languages and StandardsXML
3 Comments1 Solution1085 ViewsLast Modified:
a post in the C# topic (URL below) was asking about finding for a node in given an xml string in order to change a certain attribute

i tested some code as a solution to the problem. the code works with other Xml strings and xml files but can't seem to work with the one he posted. on my debugging, i am suspecting errors caused by namespaces bcoz if i do a quickwatch and do a quick watch on the loaded XmlDocument, the nodes are there

here's is the code

// -- main code snippet starts here

string _xmlString = "<the string in question in the URL below>";

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(_myString);
XmlNode root = xmlDoc.DocumentElement;

XmlNamespaceManager nsmngr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmngr = GetNamespaces(xmlDoc); // -- a function (definition on the bottom part)

XmlNodeList nodeList;
nodeList = root.SelectNodes("//row",nsmngr); // -- also tried using "data/insert/row" or "//row" which i believe should return
                                                                   // -- all nodes with "row" tags

textBox1.Text = Convert.ToString(nodeList.Count);

// -- textBox1 shows "0"
// -- main code snippet ends

// -- function mentioned above
public static XmlNamespaceManager GetNamespaces(XmlDocument doc)
{
            XmlNamespaceManager namespaceManager =
                            new XmlNamespaceManager(new NameTable());

            XPathNavigator nav2 = doc.DocumentElement.CreateNavigator();

            if (nav2.MoveToFirstNamespace())
            {
                do
                {
                    if (! nav2.Name.StartsWith("xml"))
                    {
                        namespaceManager.AddNamespace(nav2.Name, nav2.Value);
                    }
                }    while (nav2.MoveToNextNamespace());
            }
            return namespaceManager;
}

// -- code ends

here is the URL of the post mentioned above, containing the xml String

https://www.experts-exchange.com/Programming/Programming_Languages/C_Sharp/Q_21541501.html

i could comphrehend a little VB.NET a bit but im a C# programmer so i would appreciate a lot if comments are done in C# as much as possible

thanks in advance..c",)
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros