Trouble using AppendChild with XmlNodeList - InfoPath Dev
in

InfoPath Dev

Trouble using AppendChild with XmlNodeList

Last post 05-11-2012 12:06 PM by Hilary Stoupa. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-11-2012 06:19 AM

    Trouble using AppendChild with XmlNodeList

    I want to add some children to every occurrence of the group "Group" in an existing XML file.  I’m using an XmlNodeList and find that the children are being added in the wrong place.

    The XML code I want to add is:

        <Graphs>
            <Graph1>
              <Graph1Image />
              <Graph1Title />
            </Graph1>
            <Graph2>

              <Graph2Image />
              <Graph2Title />
              </Graph2>
            <Graph3>
                <Graph3Image />

                <Graph3Title />
            </Graph3>
            <Graph4>
                <Graph4Image />
                <Graph4Title />
            </Graph4>

        </Graphs>

    The test file I’m using has two occurrences of the group "Group".  Instead of getting the above inserted once per Group, I’m getting this inserted in the second occurrence of Group:

          <Graphs>
            <Graph1>
                <Graph1Image />
                <Graph1Title />
                <Graph1Image />
                <Graph1Title />
            </Graph1>
            <Graph2>
                <Graph2Image />
                <Graph2Title />
                <Graph2Image />
                <Graph2Title />
            </Graph2>
            <Graph3>
                <Graph3Image />
                <Graph3Title />
                <Graph3Image />
                <Graph3Title />
            </Graph3>
            <Graph4>
                <Graph4Image />
                <Graph4Title />
                <Graph4Image />
                <Graph4Title />
            </Graph4>
            <Graph1 />
            <Graph2 />
            <Graph3 />
            <Graph4 />
        </Graphs>

    The C# code is:

        using System;
        using System.IO;
        using System.Xml;

        public class PRA
        {
            static void Main()
            {
                XmlDocument testDoc = new XmlDocument();
                testDoc.Load("C:\\Users\\Paul_R_Anderson\\Desktop\\test.xml");
                String groupString = "/SAN/EQLCollection/EQLArray/Groups/Group";
                XmlElement graphsElement = testDoc.CreateElement("Graphs");
                XmlNodeList xnList = testDoc.SelectNodes(groupString);
           
                foreach (XmlNode groupNode in xnList)
                {
                    groupNode.AppendChild(graphsElement);

                    for (int j = 1; j < 5; j++)
                    {
                        String graphString = String.Concat(groupString, "/Graphs");
                        String graphNumber = String.Concat("Graph", j);
                        XmlNode graphNode = testDoc.SelectSingleNode(graphString);
                        XmlElement graphElement = testDoc.CreateElement(graphNumber);
                   
                        graphNode.AppendChild(graphElement);

                        String childString =
    String.Concat(groupString, "/Graphs/", graphNumber);
                        XmlNode childNode = testDoc.SelectSingleNode(childString);
                        String graphTitle = String.Concat(graphNumber, "Title");
                        String graphImage = String.Concat(graphNumber, "Image");
                        XmlElement titleElement = testDoc.CreateElement(graphTitle);
                        XmlElement imageElement = testDoc.CreateElement(graphImage);

                        childNode.AppendChild(imageElement);
                        childNode.AppendChild(titleElement);
                    }
                }
                testDoc.Save("C:\\Users\\Paul_R_Anderson\\Desktop\\testafter.xml");
            }
        }

    I could sure use an idea about where my code has gone awry!

    Paul

  • 05-11-2012 12:06 PM In reply to

    Re: Trouble using AppendChild with XmlNodeList

    Off hand, I think it may because you are going back to the testDoc variable to create elements and select things - instead of just working with the current graphNode - does that make sense?

    Your graphsElement needs to be instantiated inside your foreach loop, too, I think - because the second time that loop runs is when you are adding all those fields to it a second time.

    Not to sound snarky - but do you know how to run your code in debug so you can see what is happening? I have a blog post here that may help - I know learning that helped me start figuring out where I was going wrong....

    Hilary Stoupa

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