Copying group of nodes from Seconday Source to Main DataSource - InfoPath Dev
in

InfoPath Dev

Copying group of nodes from Seconday Source to Main DataSource

Last post 08-26-2009 01:37 PM by witzer. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 08-07-2009 08:13 AM

    Copying group of nodes from Seconday Source to Main DataSource

    Great forum!

     

    Via a webservice I am populating a secondary data source.  Items consists of LineItems and there can be 0 or many LineItems, and there are many other nodes under LineItems.

    So in code I am trying the following below and it bombs at the last line.  Should I be using the XmlNodeList Class instead or should I be cloning nodes in a foreach loop?  Just not sure what I am missing here.  Thank you very much. 

    XPathNavigator MainSource = this.CreateNavigator();

    XmlNamespaceManager ns = this.NamespaceManager;

    //secondary data source

    XPathNavigator SecSource = DataSources["DI_GetDIDoc"].CreateNavigator();SecSource.SelectSingleNode("/dfs:myFields/dfs:dataFields/ns2:DI_GetDIDocResponse/ns2:DI_GetDIDocResult/JDEDataList/ns10:Items", NamespaceManager);

    //selecting items node at the main data source 

    MainSource.SelectSingleNode(
    "/ns10:DirectImportDBForm/ns10:Items", ns);

     

    if (!SecSource.HasChildren)

    return;

    else

    {

    SecSource.MoveToChild(
    "LineItem", "");

    MainSource.AppendChild(SecSource);

  • 08-08-2009 09:50 AM In reply to

    Re: Copying group of nodes from Seconday Source to Main DataSource

    So the namespaces in your xpath look interesting to me. Is your main data source using ns10 namespace? Are you using the Copy XPath function in the data source task pane to get your XPaths?

    That being said, check out this post, this one, and do a search on XPathNodeIterator to get some other ideas.

    Hilary Stoupa

  • 08-10-2009 08:35 AM In reply to

    Re: Copying group of nodes from Seconday Source to Main DataSource

    Thanks Hillary - but I think there has to be an easier way to do this.  All the paths are pasted in after I select the 'Copy XPath'.  I am much more familiar with IP 2003 OM then 2007.  In 2003, all I needed to do was write the following code:

    secondaryDSDOM.setProperty("SelectionNamespaces","xmlns:ns1=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2006-08-29T12-15-49\" xmlns:dfs=\"http://schemas.microsoft.com/office/infopath/2003/dataFormSolution\" xmlns:ns2=\"http://mywilton.com/internalwebservices/formsdb2data\" xmlns:my=\"http://schemas.microsoft.com/office/infopath/2003/myXSD/2008-03-24T19:02:38\" xmlns:ns3=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"");

    IXMLDOMNodeList nodeListItems = secondaryDSDOM.selectNodes("//JDEDataList/ns1:Items/ns1:LineItem");

    IXMLDOMNodeList nodeMainList = nodeMainDOM.selectNodes("/ns1:DirectImportDBForm/ns1:Items/ns1:LineItem");IXMLDOMNode nodeMain = nodeMainDOM.selectSingleNode("/ns1:DirectImportDBForm/ns1:Items");

     

    //Remove any pre-existing nodes (used mainly when "Refresh" button is clicked)

    foreach(IXMLDOMNode node3 in nodeMainList)

    {

    node3.parentNode.removeChild(node3);

    }

     

    //Add nodes from JDEDataList

    foreach(IXMLDOMNode node in nodeListItems)

    {

    IXMLDOMNode nodeCloneItem = node.cloneNode(true);

     

    nodeMain.appendChild(nodeCloneItem);

    }

     

    //Remove any blank nodes

    IXMLDOMNodeList nodeList2 = nodeMainDOM.selectNodes("/ns1:DirectImportDBForm/ns1:Items/ns1:LineItems[SKU=\"\"]");foreach(IXMLDOMNode node2 in nodeList2)

    {

    node2.parentNode.removeChild(node2);

    }

    but when I try finding the right classes to use based on the 2007 OM, I'm always getting errors.

  • 08-10-2009 09:07 AM In reply to

    Re: Copying group of nodes from Seconday Source to Main DataSource

     There are a few problematic things in the code in your first post.  Firstly, this line:

      MainSource.SelectSingleNode("/ns10:DirectImportDBForm/ns10:Items", ns);

    Will return a reference to an XPathNavigator positioned at the ns10:Items node (assuming it exists).  It will not change the position of the MainSource variable, which will remain positioned at the document root of the main data source.  You need to do something like this:

    XPathNavigator mainItems =  MainSource.SelectSingleNode("/ns10:DirectImportDBForm/ns10:Items", ns);

     Even with that resolved, it looks like your code would only append a single line item inside that else block, and not several if there are several.  I'm not positive that it will work, but please try changing this:

    if (!SecSource.HasChildren)return;

    else

    {

    SecSource.MoveToChild(
    "LineItem", "");

    MainSource.AppendChild(SecSource);

    To this:

    foreach(XPathNavigator lineItem in SecSource.Select("LineItem"))
    {
         mainItems.AppendChild(lineItem);
    }

     

    Jimmy Rishe / Software Developer / Microsoft MVP
    Qdabra Software
  • 08-26-2009 01:37 PM In reply to

    Re: Copying group of nodes from Seconday Source to Main DataSource

    Worked well Jimmy, thanks.  There is a problem I can't figure out though with that solution.  The mainItems datasource is a repeating section.  When I debug, I get a value where the count = 2 for the SecSource and yet it always displays 3 rows in the repeating section (one is always blank) when there should only be 2.  It also only enters the body of the foreach loop 2 times (appending twice) so how come I always get 3 rows?

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