Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 26th, 2006, 05:15 PM
Ramya A
Guest
 
Posts: n/a
Default How to create nested nodes in XML in C#

Hi,

I have to create an XML in the following syntax:

<RESPONSE>
<QueueItems>
<Node1>.....
<Node2>.....
</QueueItems>
<QueueItems>
...
</QueueItems>
</RESPONSE>

Iam building this through a SqlDataReader object. My problem is I
cannot get DOM object to create the QueueItems node. Please help!

TIA
Ramya

  #2  
Old July 26th, 2006, 07:15 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: How to create nested nodes in XML in C#



Ramya A wrote:

Quote:
I have to create an XML in the following syntax:
>
<RESPONSE>
<QueueItems>
<Node1>.....
<Node2>.....
</QueueItems>
<QueueItems>
...
</QueueItems>
</RESPONSE>
>
Iam building this through a SqlDataReader object. My problem is I
cannot get DOM object to create the QueueItems node. Please help!
If you do
sqlCommand.ExecuteXmlReader
instead of
sqlCommand.ExecuteReader
then you have an XmlReader returned which you can directly feed to the
Load method of an XmlDocument. You will need to use a Transact-SQL
statement with a valid FOR XML clause.

If that does not help then you need to use the DOM the proper way e.g.
XmlDocument xmlDocument = new XmlDocument();
XmlElement response = xmlDocument.CreateElement("RESPONSE");
XmlElement items = xmlDocument.CreateElement("QueueItems");
XmlElement node = xmlDocument.CreateElement("Node1");
items.AppendChild(node);
response.AppendChild(items);
xmlDocument.AppendChild(response);
So the proper way is to use CreateElement on the document itself to
create the element node and then use AppendChild on the node you want
the element to be a child of.



--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
  #3  
Old July 26th, 2006, 07:55 PM
Ramya A
Guest
 
Posts: n/a
Default Re: How to create nested nodes in XML in C#

Hi Martin,

Your second solution worked. I used the DOM the proper way. Thanks a
lot.

Ramya
Martin Honnen wrote:
Quote:
Ramya A wrote:
>
>
Quote:
I have to create an XML in the following syntax:

<RESPONSE>
<QueueItems>
<Node1>.....
<Node2>.....
</QueueItems>
<QueueItems>
...
</QueueItems>
</RESPONSE>

Iam building this through a SqlDataReader object. My problem is I
cannot get DOM object to create the QueueItems node. Please help!
>
If you do
sqlCommand.ExecuteXmlReader
instead of
sqlCommand.ExecuteReader
then you have an XmlReader returned which you can directly feed to the
Load method of an XmlDocument. You will need to use a Transact-SQL
statement with a valid FOR XML clause.
>
If that does not help then you need to use the DOM the proper way e.g.
XmlDocument xmlDocument = new XmlDocument();
XmlElement response = xmlDocument.CreateElement("RESPONSE");
XmlElement items = xmlDocument.CreateElement("QueueItems");
XmlElement node = xmlDocument.CreateElement("Node1");
items.AppendChild(node);
response.AppendChild(items);
xmlDocument.AppendChild(response);
So the proper way is to use CreateElement on the document itself to
create the element node and then use AppendChild on the node you want
the element to be a child of.
>
>
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles