Iterate thru nodes and get value - InfoPath Dev
in

InfoPath Dev

Iterate thru nodes and get value

Last post 12-15-2011 07:51 AM by jaxkookie. 0 replies.
Page 1 of 1 (1 items)
Sort Posts: Previous Next
  • 12-15-2011 07:51 AM

    Iterate thru nodes and get value

    I posted the other day trying to find a solution to an issue. Now that I am a bit farther I have an issue I can seem to figure out. The requirements are to round trip data. I have a master section (non-repeating) and a child section (repeating table). I use the master to write or append values into the child. Eventually I may want to delete or edit an item. So using rules I write the value from the repeating table row into the Master fields. in the master I have an option to delete the item or update it. deleting is easy. updating was easy and on one test it worked. some reason going through my foreach loop, although the count is correct it seems to only return the value of the first child.

    
     public void TestMe_Clicked(object sender, ClickedEventArgs e)
            {
                XPathNavigator root = MainDataSource.CreateNavigator();
                string xpathM = "/my:myFields/my:Master";
                string xpathC = "/my:myFields/my:Chld/my:Child/my:Child";
    
                XPathNavigator Master1 = root.SelectSingleNode(xpathM + "1", NamespaceManager);
                XPathNavigator Master2 = root.SelectSingleNode(xpathM + "2", NamespaceManager);
                XPathNavigator Master3 = root.SelectSingleNode(xpathM + "3", NamespaceManager);
                XPathNavigator Master4 = root.SelectSingleNode(xpathM + "4", NamespaceManager);
                XPathNavigator Master5 = root.SelectSingleNode(xpathM + "5", NamespaceManager);
                XPathNavigator Master6 = root.SelectSingleNode(xpathM + "6", NamespaceManager);
                XPathNavigator index = root.SelectSingleNode("/my:myFields/my:index", NamespaceManager);
                XPathNavigator Btn_lbl = root.SelectSingleNode("/my:myFields/my:Btn_lbl", NamespaceManager);
                XPathNavigator ChildrowNum = root.SelectSingleNode("/my:myFields/my:ChildrowNum", NamespaceManager);
                XPathNavigator Child1 = root.SelectSingleNode(xpathC + "1", NamespaceManager);
                XPathNavigator Child2 = root.SelectSingleNode(xpathC + "2", NamespaceManager);
                XPathNavigator Child3 = root.SelectSingleNode(xpathC + "3", NamespaceManager);
                XPathNavigator Child4 = root.SelectSingleNode(xpathC + "4", NamespaceManager);
                XPathNavigator Child5 = root.SelectSingleNode(xpathC + "5", NamespaceManager);
                XPathNavigator Child6 = root.SelectSingleNode(xpathC + "6", NamespaceManager);
                XPathNavigator rowNum = root.SelectSingleNode("/my:myFields/my:Chld/my:Child/my:rowNum", NamespaceManager);
    
                string xpathChld = "/my:myFields/my:Chld[1]";
                string xpathrowNum1 = "//my:Child/my:rowNum[1]";
                XPathNavigator nChld = MainDataSource.CreateNavigator().SelectSingleNode(xpathChld, NamespaceManager);
                XPathNodeIterator children = nChld.SelectChildren(XPathNodeType.Element);
                
                // Loop through collection and display information.
                if (Btn_lbl.Value.ToString().Contains("Update"))
                {
                    
                    foreach (XPathNavigator selectedNode in children)
                    {
                        XPathNavigator n2 = selectedNode.SelectSingleNode(xpathrowNum1, NamespaceManager);
                        if (n2.Value == ChildrowNum.Value)
                        {
                            Child1.SetValue(Master1.Value.ToString());
                            Child2.SetValue(Master2.Value.ToString());
                            Child3.SetValue(Master3.Value.ToString());
                            Child4.SetValue(Master4.Value.ToString());
                            Child5.SetValue(Master5.Value.ToString());
                            Child6.SetValue(Master6.Value.ToString());
                        }
                    }
    
    
                    //XPathNodeIterator rows = root.Select(xpathChld, NamespaceManager);
    
                    //while (rows.MoveNext())
                    //{
                    //    string field1 = rows.Current.SelectSingleNode(
                    //    "my:rowNum", NamespaceManager).Value;
                    //     if (field1 == ChildrowNum.Value)
                    //     {
                    //         Child1.SetValue(Master1.Value.ToString());
                    //         Child2.SetValue(Master2.Value.ToString());
                    //         Child3.SetValue(Master3.Value.ToString());
                    //         Child4.SetValue(Master4.Value.ToString());
                    //         Child5.SetValue(Master5.Value.ToString());
                    //         Child6.SetValue(Master6.Value.ToString());
                    //         break;
                    //     }
                         
                    //} 
    
                }
    
    
               
    
                else
                {
    
    
                    XPathNavigator lastrow = root.SelectSingleNode("//my:Child[last()]", NamespaceManager);
                    string lstrw = lastrow.SelectSingleNode("my:rowNum", NamespaceManager).Value.ToString();
                   
    
                    XPathNavigator C1 = root.SelectSingleNode("/my:myFields/my:Chld/my:Child/my:Child1", NamespaceManager);
                    XPathNavigator C1Parent = root.SelectSingleNode("/my:myFields/my:Chld/my:Child[1]", NamespaceManager);
    
                    int i = 1;
                    i = Convert.ToInt32(lstrw) + 1;
    
                    if (C1.Value == "0")
                    {
                        if (C1Parent != null)
                        {
                           
    
                            Child1.SetValue(Master1.Value.ToString());
                            Child2.SetValue(Master2.Value.ToString());
                            Child3.SetValue(Master3.Value.ToString());
                            Child4.SetValue(Master4.Value.ToString());
                            Child5.SetValue(Master5.Value.ToString());
                            Child6.SetValue(Master6.Value.ToString());
                            rowNum.SetValue(i.ToString());
    
                        }
    
                    }
                    else
                    {
                        XPathNavigator navigator = MainDataSource.CreateNavigator();
                        navigator.MoveToFollowing(XPathNodeType.Element);
    
                        XPathNavigator sectionNode = navigator.SelectSingleNode("/my:myFields/my:Chld", NamespaceManager);
    
                        string ns = sectionNode.NamespaceURI;
    
                        XmlWriter writer = sectionNode.AppendChild();
    
                        writer.WriteStartElement("my", "Child", ns);
                        writer.WriteElementString("my", "Child1", ns, Master1.Value.ToString());
                        writer.WriteElementString("my", "Child2", ns, Master2.Value.ToString());
                        writer.WriteElementString("my", "Child3", ns, Master3.Value.ToString());
                        writer.WriteElementString("my", "Child4", ns, Master4.Value.ToString());
                        writer.WriteElementString("my", "Child5", ns, Master5.Value.ToString());
                        writer.WriteElementString("my", "Child6", ns, Master6.Value.ToString());
                        writer.WriteElementString("my", "rowNum", ns, i.ToString());
    
                        writer.WriteEndElement();
    
                        writer.Close();
                    }
    
    
                }
    
                index.SetValue("1");
               
            }
    

    Field layout

    myFields
       Master1
       Master2
       ...
       Chld
          Child
             Child1
             Child2
             ...
    
    few other fields
    
    

    Maybe it is my lack of understand of XPATH, but n2 seems to only be returning 1 as a value. as each row is inserted the next number is assigned. When it writes the value to the master the rowNum maatches ChildrowNum, but in the foeach loop it is only 1. in the while loop commented out it matches, but none of the fields are updated. Can any one point out what I am missing?

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