Some time ago, a form with multiple views in the project must use code to complete some special functions. The information on the Internet is not easy to find and can be sorted out. Note: The infopath encoding method is special. Controls are bound to data sources, and data sources are modified instead of control attributes.
1. Obtain non-primary data sources
Xpathnavigator mynavigator = This. datasources ["secondarydatasource"]. createnavigator ();
MessageBox. Show (mynavigator. outerxml. tostring (), "secondary data source XML ");
2. obtain information about the current view.
If (this. currentview. viewinfo. Name = "viewname") // you can specify the name of the current view.
{}
In the public void formevents_viewswitched (Object sender, viewswitchedeventargs e) event, you can use the above judgment to operate a specific view.
3. Get/set the value of a data source,
Xpathnavigator root = This. maindatasource. createnavigator ();
Xpathnavigator namefield = root. selectsinglenode ("/My: myfields/My: mainname/My: Name", namespacemanager );
Namefield. setvalue ("hello ");
String>
4. traverse the repeat control to obtain user input data
String message = "";
Xpathnavigator root = maindatasource. createnavigator ();
Xpathnodeiterator namenodes = root. Select ("/My: myfields/My: mainname/My: Name", namespacemanager );
While (namenodes. movenext ())
{
Message + = namenodes. Current. Value + system. environment. newline;
}
MessageBox. Show (Message );
5. dynamically add controls
Currentview. executeaction (actiontype. xcollectioninsert, "group_1 ");
6. Make the text value corresponding to the data source in the selected state
Xpathnavigator textnode = createnavigator (). selectsinglenode ("/My: myfields/My: field1", namespacemanager );
Currentview. selecttext (textnode );
Currentview. executeaction (actiontype. Cut );
7. Call WebService
// Open connection.
Webserviceconnection WSC = (webserviceconnection) This. dataconnections ["Main query"];
// Create xmldocuments.
Xmldocument inputdocument = new xmldocument ();
Xmldocument outputdocument = new xmldocument ();
Xmldocument errorsdocument = new xmldocument ();
// Load input document.
Inputdocument. loadxml ("<inputvalue1> test </inputvalue1> <inputvalue2> 5 </inputvalue2> ");
// Create xpathnavigator objects for documents.
Xpathnavigator inputnav = inputdocument. createnavigator ();
Xpathnavigator outputnav = outputdocument. createnavigator ();
Xpathnavigator errorsnav = errorsdocument. createnavigator ();
// Call execute method.
WSC. Execute (inputnav, outputnav, errorsnav );
When retrieving data in the repeat control, I use dataset for storage, because I found that the values of the subcontrol under each repeat cannot be traversed. For example. The repeat datatable has three subcontrols, And the Textbox Control has three values. The corresponding textbox value cannot be obtained when the datatable is traversed through the xpathnodeiterator instance. By default, the value is obtained for the first time. Therefore, we have to use datarow to traverse different columns and assign values respectively. I don't know who has a better method in the garden. Please let me know. Thank you.