Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2452
  • Last Modified:

XmlNode array to XmlDocument

I have an array of XmlNodes that I want to put into an XmlDocument so I can parse through them easily and save the XML file.

Suggestions?
0
eric0213
Asked:
eric0213
  • 5
  • 3
1 Solution
 
ozymandiasCommented:
Create an XMLDocument.
Add a root element (called root ?).
Loop through your array and add the nodes to the root as children.
0
 
eric0213Author Commented:
But what about the childnodes that have childnodes of their own?  I can write a recursive function to do this, but I thought there might be an easier way of doing this.

Something like
XmlDocument x = New XmlDocument();
x.AddNode("root");
x.ChildNodes = arrXmlNodes;

But this doesn't work, so I was hoping for somethign similar, something built-in.
0
 
ozymandiasCommented:
using System;
using System.Xml;

class XMLTest2{

      public static void Main(string[] args){

            // create dummy document where we originally got our nodes from
            XmlDocument doc1 = new XmlDocument();
            // create an array6 to hold our nodes
            XmlNode[] nodes = new XmlNode[10];
            for (int i = 0; i < 10; i++){
                  nodes[i] = doc1.CreateElement("node" + i.ToString());
            }

            // now we create a document to add the nodes to
            XmlDocument doc2 = new XmlDocument();
            // preload it with a root element
            doc2.LoadXml("<root/>");
            // get a reference to the root element
            XmlNode root = doc2.DocumentElement;
            //loop through the array and add the nodes to the root having imported them into the document context
            for (int i = 0; i < 10; i++){
                  root.AppendChild(doc2.ImportNode(nodes[i],true));
            }
            Console.WriteLine(root.OuterXml);

      }


}
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ozymandiasCommented:
Sorry, didn't see your comment there....


        doc2.ImportNode(nodes[i],true)

The true in the above import method specifies that the import should be "deep", i.e. it should incluide all child nodes.
0
 
eric0213Author Commented:
Excellent!  I'll give this a try when I get home from work and let you know how it goes.

Thanks
0
 
eric0213Author Commented:
I haven't tested the solution because I ran into another problem.  Let's just assume your solution works.  If not, I'll come knocking.

Thanks again for your help.
Eric
0
 
ozymandiasCommented:
Did you ever get this working ?
Can you grade this question now ?
0
 
ozymandiasCommented:
Thanks :)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now