How do I – Programmatically select a field inside InfoPath XML using C#

Written by Cornelius J. van Dyk on . Posted in How Do I...

As we know, InfoPath forms are actually XML documents behind the scenes… a special kind of XML, but XML nonetheless. As such we should be able to figure out how to change the value of a field pretty easily, right? You’d think so, but unless you live inside XML every day (so few of us do these days) it might now be as easy as you’d have thought. After getting the InfoPath document loaded to an XmlDocument object, one would expect a statement thus:

doc.SelectSingleNode("//my:Family", nsm).InnerText = "New Value";
to have the desired effect. Alas, it’s not that simple. It never is, is it? Smile Instead, this is what you’re greeted with: image_thumb_38C22EAC What we need to do is ensure that your XmlNamespaceManager is properly assigned with the content from the given InfoPath form in order to translate the “my:” part of our xPath statement. If we take a look at the InfoPath document’s OuterXml value we see this: Untitled-1 With so many xmlns attributes in the InfoPath document, we need some automation to prepare our XmlNamespaceManager so we can interact with the data properly. Adding this little code snippet to our existing code, should resolve that nasty error:
foreach (XmlAttribute att in doc.DocumentElement.Attributes)
  if (att.Prefix == "xmlns")
    nsm.AddNamespace(att.LocalName, att.Value);
Remember to create the XmlNamespaceManager object with a reference to the base XmlDocument’s NameTable thus:
nsm = new XmlNamespaceManager(doc.NameTable);
Throw all that together and we are now able to manipulate the InfoPath field programmatically to our heart’s content.



Tags: , , ,

Trackback from your site.

Cornelius J. van Dyk

Born and raised in South Africa during the 70's I got my start in computers when a game on my Sinclair ZX Spectrum crashed, revealing it's BASIC source code. The ZX had a whopping 48K of memory which was considered to be a lot in the Commodore Vic20 era, but more importantly, it had BASIC built into the soft touch keyboard. Teaching myself to program, I coded my first commercial program at age 15.

After graduating high school at 17, I joined the South African Air Force, graduating the Academy and becoming a Pilot with the rank of First Lieutenant by age 20. After serving my country for six years, I made my way back into computer software.

Continuing my education, I graduated Suma Cum Laude from the Computer Training Institute before joining First National Bank where my work won the Smithsonian Award for Technological Innovation in the field of Banking and Insurance. Soon I met Will Coleman from Amdahl SA, who introduced me to a little known programming language named Huron/ObjectStar. As fate would have it, this unknown language and Y2K brought me to the USA in 1998.

I got involved with SharePoint after playing around with the Beta for SharePoint Portal Server 2003. Leaving my career at Rexnord to become a consultant in 2004, I was first awarded the Microsoft Most Valuable Professional Award for SharePoint in 2005, becoming only the 9th MVP for WSS at the time. I fulfilled a life long dream by pledging allegiance to the Flag as a US citizen in 2006. I met the love of my life and became a private consultant in 2008. I was honored to receive my ninth MVP award for SharePoint Server in 2013.

Leave a comment

You must be logged in to post a comment.