Winsmarts.com

Microsoft MVP

MVP Logo

Awarded the Microsoft MVP Award.

Hosted By

blah!bLaH!BLOG!!

Accessing Infopath fields, a simpler way.

Posted on 12/1/2007 @ 2:55 AM in #Sharepoint | 2 comments | 3760 views

What is the conventional way you'd access fields in an infopath form in VSTA code?

XPathNavigator myNavigator =
    this.MainDataSource.CreateNavigator().SelectSingleNode( "/my:myFields/my:customerName", NamespaceManager);

The challenge here is that complex, and I'd argue, unmaintainable XPATH that you need to get by right clicking on each property in a given datasource, and choosing "Copy XPath".

Is there a better way?

Umm .. yeah!

Here's what you do.

  • Open the published form in Design mode.
  • Choose File --> Save as source files.
  • Delete everything except myschema.xsd
  • Open the myschema.xsd, and add "nillable=true" on all xsd:elements, you have to do this because MSFT's XML doesn't match W3C's.
  • run xsd.exe myschema.xsd /c to generate a myschema.cs
  • Add the myschema.cs to your VSTA project.
  • Now use strongly typed code as shown below:

XmlSerializer serializer = new XmlSerializer(typeof(myFields));

XmlTextReader rdrMyFields = new XmlTextReader(
    new System.IO.StringReader(
    this.CreateNavigator().SelectSingleNode(
    "/my:myFields", NamespaceManager).OuterXml));
myFields fields = (myFields)serializer.Deserialize(rdrMyFields);
fields.customerName = "Sahil Malik";

Cool huh? :-)

Where this really shines is, if you have binary fields, such as an image.


On 2/29/2008 5:43:53 PM Jason said ..
This is great... I am just trying to get it working in vb though. I also submit my InfoPath form as the "my:myfields" tree + sub nodes. Not sure if this makes a difference... Somehow I am doing this wrong but:
- "newform" is the object passed (by infopath) as instance of the class created by xsd.exe /c "InfoPathScannerRequestService" is the namespace, "myFields" is the class name. This is what I have: Dim serializer As New XmlSerializer(GetType(InfoPathScanningRequestService.myFields))
Dim rdrMyFields As New XmlTextReader(New System.IO.StringReader(Me.CreateNavigator.SelectSingleNode("/my:myFields", NamespaceManager).OuterXml))
Dim fields As myFields = DirectCast(serializer.Deserialize(rdrMyFields), myFields)
fields.customerName = "Sahil Malik" The "Me.CreateNavigator" and "NamespaceManager" are not defined. I read that the .CreateNavigator method is a member of XmlNode but I don't have one because the xml was typed to the class "myFields". Any help?

On 6/10/2008 1:59:48 PM CodeGator said ..
Hello Jason, Be sure your code imports System.Xml. Also, make sure you are trying to run this code from the form's code-behind. (FormCode). Do those two things and it should compile. Here is my attempt, that I just compiled: Dim s As New XmlSerializer(Type.GetType("myFields"))
Dim r As New XmlTextReader(New StringReader(Me.CreateNavigator().SelectSingleNode("/my:myFields", NamespaceManager).OuterXml))
Dim m As myFields = s.Deserialize(r)
m.field1 = "100" I hope that helps, Martin

Please post your comments:


Your feedback will be submitted for moderation, and will appear after it is approved.

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Site designed and maintained by Sahil Malik | All Rights Reserved. ©2007 WinSmarts.com.