Solved

XmlNode array to XmlDocument

Posted on 2004-03-24
8
2,241 Views
Last Modified: 2012-06-27
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
Comment
Question by:eric0213
  • 5
  • 3
8 Comments
 
LVL 15

Expert Comment

by:ozymandias
ID: 10671931
Create an XMLDocument.
Add a root element (called root ?).
Loop through your array and add the nodes to the root as children.
0
 
LVL 4

Author Comment

by:eric0213
ID: 10672073
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 10672076
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 15

Accepted Solution

by:
ozymandias earned 125 total points
ID: 10672084
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
 
LVL 4

Author Comment

by:eric0213
ID: 10672125
Excellent!  I'll give this a try when I get home from work and let you know how it goes.

Thanks
0
 
LVL 4

Author Comment

by:eric0213
ID: 10678303
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 11054208
Did you ever get this working ?
Can you grade this question now ?
0
 
LVL 15

Expert Comment

by:ozymandias
ID: 11054280
Thanks :)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question