Can i create a XmlNamespaceManager object from the xml file i am about to read?

Buzzby picture Buzzby · Jan 11, 2011 · Viewed 7.4k times · Source

I have some c# code running on sharepoint that i use to check inside the xml of an infopath document to see if i should checking the document or discard the document.

The code is working fine for a couple of different form templates i have created but is failing on my latested one. I have discovered that the XmlNamespaceManager i am creating contains the wrong diffinition for the "MY" namepsace.

I'll try to explain

I have this code to decalre my XmlNamespaceManager

NameTable nt = new NameTable();
NamespaceManager = new XmlNamespaceManager(nt);

// Add prefix/namespace pairs to the XmlNamespaceManager.
NamespaceManager.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
NamespaceManager.AddNamespace("xhtml", "http://www.w3.org/1999/xhtml");
NamespaceManager.AddNamespace("dfs", "http://schemas.microsoft.com/office/infopath/2003/dataFormSolution");            
NamespaceManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-07-14T13:45:59");
NamespaceManager.AddNamespace("xd", "http://schemas.microsoft.com/office/infopath/2003");`

I can then use the following line of code to search for the xml i am after

XPathNavigator nav = xml.CreateNavigator().SelectSingleNode("//my:HasSaved", NamespaceManager);

nav.Value then gets be the data i want.

This all works fine on a couple of my form templates. I ahve a new form template and have discovered that i need to use thi line instead

NamespaceManager.AddNamespace("my", "http://schemas.microsoft.com/office/infopath/2003/myXSD/2010-11-30T17:39:37");

The date is different.

My problem is that i cannot add this twice as only 1 set of forms will work.

So my question is. Is there a way i can generate the NamespaceManager object from the XML file as this is all contained in the header? I have not been able to find a simple way round this.

Answer

Buzzby picture Buzzby · Feb 10, 2011

I found a way of doing this. Instead of adding the "my" namespace it can be pulled from the XmlDocument object. This might just be a bit of luck that it works this way but i'm happy with it.

NamespaceManager.AddNamespace("my", formXml.DocumentElement.NamespaceURI

formXML is an XmlDocument created from the infopath XML