1: private string GetNodeValueFromMain(string xpath)
2: {
3: string retValue = string.Empty;
4: XPathNavigator retNode = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
5: if (retNode != null)
6: {
7: retValue = retNode.InnerXml;
8: }
9: return retValue;
10: }
Occasionally, the node value that you are retrieving is relative to a particular event object site (for example, a sibling node in a Changed event). In the GetNodeValueFromEvent function, the event object is passed as an argument along with the XPath statement.
1: private string GetNodeValueFromEvent(XmlEventArgs eventObject, string xpath)
2: {
3: string retValue = string.Empty; XPathNavigator retNode = eventObject.Site.SelectSingleNode(xpath, NamespaceManager);
4: if (retNode != null)
5: {
6: retValue = retNode.InnerXml;
7: }
8: return retValue;
9: }
To retrieve a node value from a secondary data source, you must pass the name of the data connection as
an argument, along with the XPath statement, as shown with the GetNodeValueFromSecondary function.
1: private string GetNodeValueFromSecondary(string source, string xpath)
2: {
3: string retValue = string.Empty; XPathNavigator retNode = DataSources[source].CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
4: if (retNode != null)
5: {
6: retValue = retNode.InnerXml;
7: }
8: return retValue;
9: }
Setting a Node Value Another task used in most managed-code solutions is setting a particular node value in the main data source. The SetNodeValueInMain function uses the first XPath statement argument to identify an XPathNavigator object and then set it to the value of the second argument.
1: private void SetNodeValueInMain(string xpath, string nodeValue)
2: {
3: XPathNavigator mainNode = MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
4: if (mainNode != null)
5: {
6: mainNode.SetValue(nodeValue);
7: }
8: }
As with retrieving values, there are occasions where you might want to set a node value relative to a particular event object. Note that during certain events (for example, the Changing event), the main data source is read-only. In any
event, the SetNodeValueInEvent function uses an event object as an additional argument.
1: private void SetNodeValueInEvent(XmlEventArgs eventObject, string xpath, string nodeValue)
2: {
3: XPathNavigator eventNode = eventObject.Site.SelectSingleNode(xpath, NamespaceManager);
4: if (eventNode != null)
5: {
6: eventNode.SetValue(nodeValue);
7: }
8: }
To set a secondary data source node value, you must pass the name of the data connection as an argument, along with the XPath statement and new node value, as shown with the SetNodeValueInSecondary function.
1: private void SetNodeValueInSecondary(string source, string xpath, string nodeValue)
2: {
3: XPathNavigator secondaryNode = DataSources[source].CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
4: if (secondaryNode != null)
5: {
6: secondaryNode.SetValue(nodeValue);
7: }
8: }
Creating a Node Many times, a repeating section or repeating table control in a form
template has items added to it programmatically during the Loading event or based on specific actions at run time. The CreateNode function takes the first XPathNavigator object argument and appends the node identified by the other three arguments.
1: private void CreateNode(XPathNavigator parentNode, string childNode, string childNodePrefix, string childNodeValue)
2: {
3: if (parentNode != null)
4: {
5: parentNode.AppendChildElement(childNodePrefix, childNode, parentNode.LookupNamespace(parentNode.Prefix), childNodeValue);
6: }
7: }
Removing Nodes Whereas creating a node and adding it to a repeating section or repeating table is a common task, so too is removing a node from one of these repeating controls. The RemoveNode function takes the first XPathNavigator object argument and removes the child node identified with the second XPath
statement argument.
1:
2: private void RemoveNode(XPathNavigator parentNode, string xpath)
3: {
4: if (parentNode != null)
5: {
6: XPathNavigator childNode = parentNode.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
7: if (childNode != null)
8: {
9: childNode.DeleteSelf();
10: }
11: }
12: }
Removing all items from a repeating section or repeating table involves a reverse iteration through
an XPathNodeIterator object. Here, the RemoveChildNodes function takes the XPathNavigator object and removes all of its child nodes.
1: private void RemoveChildNodes(XPathNavigator parentNode)
2: {
3: if (parentNode != null)
4: {
5: XPathNodeIterator childNodes = parentNode.CreateNavigator().Select("*", NamespaceManager);
6: for (int i = childNodes.Count - 1; i >= 0; i--)
7: {
8: XPathNavigator childNode = parentNode.CreateNavigator().SelectSingleNode("*[last()]", NamespaceManager);
9: childNode.DeleteSelf();
10: }
11: }
12: }

No comments:
Post a Comment