.NETGURU
Using MS DOM to create XML
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngxml' list.


Marcelo Areal
I hope anybody can help me with this problem. I have a class that takes
different parameters (all strings) and puts them into the DOM, with the
following format:

<root>
<node1>1</node1>
<node2>2</node2>
</root>

What I need now is to insert a sub-tree (newnode) one node at the time, like
this:

<root>
<node1>1</node1>
<newnode>
<newnode1></newnode1>
<newnode2></newnode2>
<newnode>
<node2>2</node2>
</root>

The code that I am using to generate the first example is:

Dim Doc As New System.Xml.XmlDocument()
Dim xmlElem As System.Xml.XmlNode

xmlElem = Doc.CreateNode(System.Xml.XmlNodeType.Element, "node1", Nothing)

xmlElem.InnerXml = "1"

Doc.DocumentElement.AppendChild(xmlElem)

Anything you can tell me will be greatly appreciated.

Marcelo.

Reply to this message...
 
    
Dan Wahlin
Using AppendChild() will add the node to the bottom of the children list
which isn't what you want of course. Instead, you'll want to use the
InsertAfter() or InsertBefore() methods if you want to add nodes as
siblings of other nodes.

First you'll have to get a reference to the node where you want to
insert new nodes after. You can use XPath to find it
(SelectSingleNode() is the method that will select one node for
you...you could just walk to the node in the DOM as well if desired) and
then once you find the node you will create the new nodes. Once the new
nodes are created you can call InsertAfter() and pass in the new node
and the node you want things to be inserted after.

The .NET SDK provides a good example of this as shown below:

using System;
using System.IO;
using System.Xml;

public class Sample
{
public static void Main()
{

XmlDocument doc = new XmlDocument();
doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
"<title>Pride And Prejudice</title>" +
"</book>");

XmlNode root = doc.DocumentElement;

//Create a new node.
XmlElement elem = doc.CreateElement("price");
elem.InnerText="19.95";

//Add the node to the document.
root.InsertAfter(elem, root.FirstChild);

Console.WriteLine("Display the modified XML...");
doc.Save(Console.Out);

}
}

HTH,
Dan Wahlin

Wahlin Consulting LLC
Microsoft MVP - ASP.NET
http://www.XMLforASP.Net: #1 ASP.NET XML Resource
XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Marcelo Areal [mailto:Click here to reveal e-mail address]
Sent: Wednesday, July 31, 2002 12:59 PM
To: aspngxml
Subject: [aspngxml] Using MS DOM to create XML

I hope anybody can help me with this problem. I have a class that takes
different parameters (all strings) and puts them into the DOM, with the
following format:

<root>
<node1>1</node1>
<node2>2</node2>
</root>

What I need now is to insert a sub-tree (newnode) one node at the time,
like
this:

<root>
<node1>1</node1>
<newnode>
<newnode1></newnode1>
<newnode2></newnode2>
<newnode>
<node2>2</node2>
</root>

The code that I am using to generate the first example is:

Dim Doc As New System.Xml.XmlDocument()
Dim xmlElem As System.Xml.XmlNode

xmlElem = Doc.CreateNode(System.Xml.XmlNodeType.Element, "node1",
Nothing)

xmlElem.InnerXml = "1"

Doc.DocumentElement.AppendChild(xmlElem)

Anything you can tell me will be greatly appreciated.

Marcelo.

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
    
Marcelo Areal
Hello Dan, and thank you one more time for responding my questions. I think
I understand what your suggestion is, but my idea was a little different.

I don't want to add nodes to an XML file (like the one in the example). I am
creating (from scratch) the XML. This is based on certain information that
may or may not be present at the moment. This is done sequentially. What I
had in mind was (and is) to find the right XML function call to be able to
add sub-trees.

Here is an example: Let's say that I find a function that allows me to
specify the "parent node" and the "new node" to be created. This will allow
me to create:

1) <newnode></newnode> within <root></root>, and then
2) <newnode1></newnode1> within <newnode></newnode>

I hope this makes sense. Thank you.

"Dan Wahlin" <Click here to reveal e-mail address> wrote in message
news:691853@aspngxml...
[Original message clipped]

Reply to this message...
 
    
Dan Wahlin
Hi Marcelo,

Actually whether you're creating the XML nodes from scratch or adding to
an existing document the same concept mentioned in the previous post
still applies. After you create the root element and append it to the
XmlDocument (which would be empty), you can then create children and
append them to the root. If you need to add children to a specific
position then you can use InsertAfter(). Otherwise AppendChild or
PrependChild will do the job. You can create a node, append children to
it, attributes, etc., and then add that node into the DOM tree.

Dan

Wahlin Consulting LLC
Microsoft MVP - ASP.NET
http://www.XMLforASP.Net: #1 ASP.NET XML Resource
XML for ASP.NET Developers by Dan Wahlin in bookstores everywhere!

-----Original Message-----
From: Marcelo Areal [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 01, 2002 6:36 AM
To: aspngxml
Subject: [aspngxml] Re: Using MS DOM to create XML

Hello Dan, and thank you one more time for responding my questions. I
think
I understand what your suggestion is, but my idea was a little
different.

I don't want to add nodes to an XML file (like the one in the example).
I am
creating (from scratch) the XML. This is based on certain information
that
may or may not be present at the moment. This is done sequentially. What
I
had in mind was (and is) to find the right XML function call to be able
to
add sub-trees.

Here is an example: Let's say that I find a function that allows me to
specify the "parent node" and the "new node" to be created. This will
allow
me to create:

1) <newnode></newnode> within <root></root>, and then
2) <newnode1></newnode1> within <newnode></newnode>

I hope this makes sense. Thank you.

"Dan Wahlin" <Click here to reveal e-mail address> wrote in message
news:691853@aspngxml...
[Original message clipped]

| [aspngxml] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/asplists/aspngxml.asp = JOIN/QUIT
| http://www.asplists.com/search = SEARCH Archives

Reply to this message...
 
 
System.Console
System.Xml.XmlDocument
System.Xml.XmlElement
System.Xml.XmlNode
System.Xml.XmlNodeType




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification