Insert a new XML node using a button control - InfoPath Dev
in

InfoPath Dev

Insert a new XML node using a button control

Last post 07-01-2005 04:01 PM by mmelonis. 2 replies.
Page 1 of 1 (3 items)
Sort Posts: Previous Next
  • 06-28-2005 05:27 PM

    Insert a new XML node using a button control

    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);
    }

  • 07-01-2005 01:15 AM In reply to

    Re: Insert a new XML node using a button control

  • 07-01-2005 04:01 PM In reply to

    Re: Insert a new XML node using a button control

    Thanks Scott. It worked;)

    Here is what my new code looks (primary additions in blue and a bunch of code deleted):


    public void CTRL1470_6_OnClick(DocActionEvent e)
    {
    // Write your code here.
    Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMDocument2 userViewDOM;
    Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNode newDataNode;
    Microsoft.Office.Interop.InfoPath.SemiTrust.IXMLDOMNodeList cpNodeList;
    string childRef;

    // Get a copy of DOM currently displayed to the user.
    userViewDOM = (IXMLDOMDocument2)thisXDocument.DOM;

    // Insert new Node.
    childRef = "//ns1:Customer/ns1:Order";

    // In the file view.xsf, the parameter for ExecuteAction can be found:
    // 1) In section properties make sure "Show insert button" is turned on
    // 2) in view.xsf :
    // xd:xmlToEdit="Order_197"
    // xd:action="xCollection::insert"
    // 3) The values above are the parameters for ExecuteAction.
    // 4) In this example, after the fact I turned off "Show insert button"
    // and the ExecuteAction continued to work.
    thisXDocument.View.ExecuteAction("xCollection::insert", "Order_197");
    cpNodeList = thisXDocument.DOM.selectNodes(childRef);


    // Find the new Order node.
    int i;
    for( i = 0; i <cpNodeList.length; i++)
    {
    if(cpNodeList.selectSingleNode(".//ns1:OrderID").text == "")
    {
    break;
    }
    }
    newDataNode = cpNodeList;

    // Preset values.
    newDataNode.selectSingleNode(".//ns1:CustomerID").text =
    newDataNode.parentNode.selectSingleNode("//ns1:Customer/nsi:CustomerID".text;
    newDataNode.selectSingleNode(".//ns1:OrderDate").text = DateTime.Now;


    }
Page 1 of 1 (3 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.