Home > work > Infopath Namespace manager creation code

Infopath Namespace manager creation code

I have had to create some code this week that reads the xml created by InfoPath. I then found that if you try and xpath on something you get a null return until the namespace manager has all of the correct entries. In the past I have created these manually but after a bit of google fo I have come up with the below piece of code. It takes an XMLDocument that you have created based on the form file (or any XML document really) and returns you a newly created namespace manager that can be used along side you xpath queries to get the data you want.



public static XmlNamespaceManager GetNameSpaceManager(XmlDocument xmlDoc)
XmlNamespaceManager ns = new XmlNamespaceManager(xmlDoc.NameTable);
foreach (XmlAttribute att in xmlDoc.DocumentElement.Attributes)
if (att.Prefix == "xmlns")
ns.AddNamespace(att.LocalName, att.Value);
return ns;

.csharpcode, .csharpcode pre
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
background-color: #f4f4f4;
width: 100%;
margin: 0em;
.csharpcode .lnum { color: #606060; }

Categories: work Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: