Thanks for the replies!
Unfortunately, even with trying the NamespaceManager code, I cannot query any XML files with a default namespace. Is there something special I have to do in that case?
If I change the namespace to something like "xmlns:default=" or "xmlns:blah=", it works fine, but the normal "xmlns=" does not work.
Something isn't right here because if you check out the value on "mgr.DefaultNamespace", it IS using the default namespace specified. This is all very confusing and almost illogical in its set up.
I'm using a file containing this data:
Code:
<?xml version="1.0"?>
<RootNode xmlns="http://blah.com/nonsensicalURL">
<Test>Testing!</Test>
</RootNode>
And my test program is the following code:
Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
namespace XmlTesting
{
class Program
{
static void Main(string[] args)
{
while (true)
{
XmlDocument doc = new XmlDocument();
doc.Load("c:\\test.xml");
XmlNamespaceManager mgr = CreateNsMgr(doc);
Console.WriteLine("Default namespace is: " + mgr.DefaultNamespace + "\r\n");
if (doc.SelectSingleNode("//RootNode", mgr) != null && doc.SelectNodes("//RootNode/Test", mgr).Count != 0)
Console.WriteLine("Query works");
else
Console.WriteLine("Query doesn't work");
Console.ReadLine();
}
}
public static XmlNamespaceManager CreateNsMgr(XmlDocument doc)
{
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
//nsmgr.AddNamespace(string.Empty, "http://blah.com/nonsensicalURL");
//nsmgr.AddNamespace("default", "http://blah.com/nonsensicalURL");
foreach (XmlAttribute attr in doc.SelectSingleNode("/*").Attributes)
{
if (attr.Prefix == string.Empty && attr.LocalName == "xmlns") nsmgr.AddNamespace(String.Empty, attr.Value);
if (attr.Prefix == "xmlns") nsmgr.AddNamespace(attr.LocalName, attr.Value);
}
return nsmgr;
}
}
}
This is driving me insane.
So I decided, "F it, I'm just removing any stupid default namespace attributes."
Well, the code below illustrates how it finds and removes the "xmlns" attribute and reloads the XML into a NEW document object since there's no "reload" method... and it STILL doesn't work.
Despite the code removing the xmlns attribute, on debug, it's STILL THERE! I even step through the code repeatedly where it checks if the xmlns attr isn't null... and after it's removed the first time, it is. Yet it still shows up in the InnerXML.
This is just beyond stupid now.
Check this madness out:
Code:
XmlDocument doc2 = new XmlDocument();
doc2.Load("c:\\test.xml");
if (doc2.DocumentElement.Attributes["xmlns"] != null)
doc2.DocumentElement.RemoveAttribute("xmlns");
XmlDocument doc = new XmlDocument();
doc.LoadXml(doc2.InnerXml);
The next step is to seriously run the InnerXml property through a Regex replace method that finds and destroys that "xmlns=" portion.
The whole reason of why I'm trying to do this... various customers of ours have data feeds set up. Most are normal XML, but a handful of em have already established feeds that use this namespace stuff.
They upload a feed to us, specify the XPath query of the nodes they need pulled out, and we do some HTML template merging stuff. The Namespace manager thing seemed to be a perfect solution up until the point I realize it doesn't work for the typical "xmlns="
[edit] Sure enough, that last method worked.
Code:
XmlDocument doc2 = new XmlDocument();
doc2.Load("c:\\test.xml");
XmlDocument doc = new XmlDocument();
doc.LoadXml(System.Text.RegularExpressions.Regex.Replace(doc2.InnerXml, @"xmlns=\"".*?\""", string.Empty));
Bah, so overkill.