I have an order entry form where the main data sources was provided via an established web services. The .xsd used by InfoPath is shown below as 'Abbreviated XML'. What I want is a button that will insert a new order record based on the xsd. Once the record is created, the code will pre-populated a few fields such as Order Date (and a few other fields not shown).
The code I have written so far is shown below as 'Code Sample".
The problem is the code only works if there is an existing Order Node. If a given customer has never created an order the code blows up, because their is not an existing Order node to clone. In other words, I want the Order structure to be an Optional, Repeating Table.
I don't understand how to create a new Order Node using the predefined Order XML structure (defined by the XML). I've researched this for a while and it seems like it should be simple, but the solution is not obvious to me. Effectively what I don't understand is how to access/load a node from the Main Data source schema. All the examples that I've seen load the structure from a file.
Any help would be much appreciated.
Abbreviate XML
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="CustomerID" />
<xs:element name="LastName" />
<xs:element name="MiddleName" />
<xs:element name="FirstName" />
<xs:element name="Order" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="OrderID" />
<xs:element name="CustomerID" />
<xs:element name="ProductName" />
<xs:element name="ProductNumber" />
<xs:element name="OrderDate" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="BillingDetail" minOccurs="0" maxOccurs="unbounded">
<xs:complexType>
<xs:sequence>
<xs:element name="BillingDetailID" />
<xs:element name="CustomerID" />
<xs:element name="ETC...." />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Sample Code:
public void CTRL1470_6_OnClick(DocActionEvent e)
{
// Write your code here.
Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMDocument2 userViewDOM;
Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode parentDataNode;
Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode newDataNode;
string parentRef, childRef;
userViewDOM = (IXMLDOMDocument2)thisXDocument.DOM;
// Get a copy of the parent node.
parentRef = "//ns1:Customer";
parentDataNode = userViewDOM.selectSingleNode(parentRef);
// ?????? How do I create a new Order Node using the predefined Order XML
// ?????? structure (defined by the XML)?
// Clone the new node that will be added.
childRef = "//ns1:Customer/ns1:Order";
newDataNode = CloneNode(userViewDOM, childRef);
// Walk the parentDataNode looking for the spot to insert the
// new node. In our case, this will were Order is found.
Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode siblingNode;
siblingNode = parentDataNode.firstChild;
while(siblingNode != null)
{
if(siblingNode.baseName != null)
{
if(siblingNode.baseName == "Order")
{
break;
}
}
siblingNode = siblingNode.nextSibling;
}
// Preset values.
newDataNode.selectSingleNode(".//ns1:CustomerID").text =
parentDataNode.selectSingleNode("//ns1:Customer/nsi:CustomerID".text;
newDataNode.selectSingleNode(".//ns1:OrderDate").text = DateTime.Now;
// Insert new Node in parent node.
parentDataNode.insertBefore(newDataNode,siblingNode);
}