473,385 Members | 2,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

Jul 26 '06 #1
2 20435


Ramya A wrote:

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/
Jul 26 '06 #2
Hi Martin,

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

Ramya
Martin Honnen wrote:
Ramya A wrote:

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/
Jul 26 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: JollyK | last post by:
Hello folks, Suppose I have a property called SortField string SortField { get { if (SortField == string.Empty) {
12
by: coosa | last post by:
Dear all, I have table called CATEGORY, which is defined as follows: CREATE TABLE CATEGORY ( CATEGORY_ID INTEGER IDENTITY(1,1) NOT NULL, CATEGORY_NAME VARCHAR(40) NOT NULL CONSTRAINT...
1
by: shanthsp2002 | last post by:
Hi friends , i am developing a setup and deployment project for our application here i have to include two other applications with our product installer, and they must be installed only when use...
2
by: Cylix | last post by:
I have try to using createElement to form a nested table, But it seems cannot show the nested table. Is it possible to do so?
4
by: Pim75 | last post by:
Hello, I have to read a XML file in ASP and save the values in a database. I can get this work, but I cannot read some nested nodes of the xml file. This is a part of the XML file: ...
3
by: quiz123 | last post by:
I am solving some old papers to improve my programming skills. I am a beginer to linked list. the question is: Define a C++ functions that create 50 nodes and return the pointer to the first...
4
by: Dmitry Kulinich | last post by:
Guys! Is there are any possibility to create nodes with duplicate names and different types in XSD? I've read the whole specification and tried in a many different ways, but not successfull. ...
1
by: rn5a | last post by:
Can someone please link me to some articles or show some e.g. on how to embed a DataGrid inside another DataGrid (child DataGrid within a parent DataGrid)? I have Googled on this topic but most...
4
by: willywongi | last post by:
Hi, I'm trying to build a filemanager for a cms. I'd like to have a page with JS that asks the server for the filelist, which in turn answers with a XML document. This is a sample of the XML...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.