Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XmlNode array to XmlDocument

Posted on 2004-03-24
8
Medium Priority
?
2,413 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
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!

 
LVL 15

Accepted Solution

by:
ozymandias earned 500 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …

916 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