Thursday, March 3, 2011

XPathNavigator & InfoPath 2010 using C#

So, back on the job and back to many problems. This time it really bugged me a bit.
I'm working on an InfoPath 2010 form, which needs some "tweaking" beyond the possibilities provided by rules implemented in InfoPath 2010.

Just as an example: I need to be able to set textfields or move their content easily. I want to be able to send the values from different checkboxes to a textfield. The checkbox is called "CheckMe" and is a boolean field. 1 means it is checked, 0 it isn't.
But how can I use these fields from InfoPath 2010? The answer is: XPathNavigator & XmlNamespaceManager.

Here's the code I'm using:

XPathNavigator xnPath = this.CreateNavigator();
XmlNamespaceManager xnNS = this.NamespaceManager;
string ContentField;
ContentField = xnPath.SelectSingleNode("//my:myFields/ContentField", xnNS).Value;
string CheckMe = xnPath.SelectSingleNode("//my:myFields/CheckMe").Value;
if (CheckMe == "1")
{
   ContentField = ContentField + "CheckMe";
}
What happens there?
XPathNavigator xnPath = this.CreateNavigator();
First of all I'm defining my "XPathNavigator" which will point to a part of my InfoPath form.

XmlNamespaceManager xnNS = this.NamespaceManager;
Here I'm creating my own new NamepaceManager, which is need in InfoPath 2010 while using C#.
Still, I'm not working with any fields. For that I gonna use this little line:
xnPath.SelectSingleNode("//my:myFields/ContentField", xnNS)
Now I'm pointing to the field I want to modify. This is a bit too long to remember, that's why I'm creating a new variable.
ContentField = xnPath.SelectSingleNode("//my:myFields/ContentField", xnNS).Value;
I need ".Value" to send the  to my variable, which in this case is a string. And now: here's the part where I'm manipulation the textfield.

if (CheckMe == "1")
{
   ContentField = ContentField + "CheckMe";
}
If the checkbox "CheckMe" is true, which means "checked", I want to write "CheckMe" into my textfield.

No comments:

Post a Comment