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(
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.