Solved

XmlNode array to XmlDocument

Posted on 2004-03-24
8
2,274 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ado.net AddWithValue when using IN condition 7 45
Where does legacy ASP.NET initialize language? 2 101
Why use this lambda? 12 95
How do I "share" on social sites? 2 38
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

732 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