Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XmlNode array to XmlDocument

Posted on 2004-03-24
8
Medium Priority
?
2,375 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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