Principle: InfoPath forms are used as templates to define the formats and resources of xml data files in items. Therefore, manipulating InfoPath data is equivalent to operating xml data files.
- Query InfoPath form elements
- Add InfoPath form elements
First, let's look at the example of using XPath for queries in common XML.
- Delete InfoPath form elements
Here we take the complex Repeating Table as an example.
The following code uses a button to traverse and search for rows with an empty field in the Repeating Table and delete a row (for details about multiple rows, see ).
public void btnDeleteBlankRS_Clicked(object sender, ClickedEventArgs e)
{
//Create an XmlNamespaceManager for ease of referencing namespaces
XmlNamespaceManager ns = this.NamespaceManager;
//Create an XPathNavigator object for the main data source
XPathNavigator xnMain = this.MainDataSource.CreateNavigator();
//Create an XPathNodeIterator object for the repeating table so we can loop thru all the rows in the repeating table
XPathNodeIterator xiRepeatingTable = xnMain.Select("/my:myFields/my:gpRepeatingTable/my:gpRepeatingTableRow", ns);
//Loop thru all repeating table rows
while (xiRepeatingTable.MoveNext())
{
//Create an XPathNodeIterator object for the repeating section nodes within the repeating table
XPathNodeIterator xiRepeatingSection = xiRepeatingTable.Current.Select("my:gpRepeatingSection/my:gpRepeatingSectionRow", ns);
//Loop thru the repeating sections within the current repeating table row
while (xiRepeatingSection.MoveNext())
{
//Create an XPathNavigator object for one of the fields within the Repeating Section
//NOTE: You may need to check more than one field to determine if the repeating section should be removed
XPathNavigator xnRepeatingSectionField = xiRepeatingSection.Current.SelectSingleNode("my:field3", ns);
//See if the field in the repeating section is empty
if (xnRepeatingSectionField.Value == string.Empty)
{
//If so, delete the current repeating section
xiRepeatingSection.Current.DeleteSelf();
}
}
}
}
string name = "User2";
string game = "Cube";
XmlNode node = doc.SelectSingleNode(String.Format("/players/player[name='{0}' and game='{1}']", name, game));
node.ParentNode.RemoveChild(node);
Or
String aaa = "/players/player[name='"+user+"' and game='"+game+"']";
XmlNode node = docc.SelectSingleNode(aaa);