473,387 Members | 1,530 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,387 software developers and data experts.

Two Deep XML?

I have some code which creates an XML output like this:

< Product>

<Product ID>1</ProductID

<ProductValue>5.00</ProductVaue>

</Product>

< Product>

<Product ID>2</ProductID

<ProductValue>10.00</ProductVaue>

</Product>

Here it is:

newElem = doc.CreateElement("Product");

childElem = doc.CreateElement("ProductID");

childElem.AppendChild(doc.CreateTextNode(ProductID .ToString()));

newElem.AppendChild(childElem);

childElem = doc.CreateElement("ProductName");

childElem.AppendChild(doc.CreateTextNode(productna me));

newElem.AppendChild(childElem);

I now want to add one more level of depth to my XML so that the output is like this (new stuff in bold):

< Product>

<Product ID>1</ProductID

<ProductValue>5.00</ProductVaue>

<Discount>

<QtyLow>1</QtyLow><QtyHigh>5</QtyHigh><Value>5%</Value>

</Discount>

<Discount>

<QtyLow>6</QtyLow><QtyHigh>10</QtyHigh><Value>10%</Value>

</Discount>

</Product>

But I'm drawing a blank as how to code this. Can anyone steer me in the right direction?

Nov 12 '05 #1
2 1034
Hello!

But I'm drawing a blank as how to code this. Can anyone steer me in the
right direction?


XmlDocument doc = new XmlDocument();
XmlElement product = doc.CreateElement("Product");
XmlElement discount = doc.CreateElement("Discount");
XmlElement qtylow = doc.CreateElement("QtyLow");
qtylow.AppendChild( doc.CreateTextNode("1") );
discount.AppendChild(qtylow);
product.AppendChild(discount);
doc.AppendChild(product);
All Elements, regardless where they are added into the tree are created
using the XmlDocument.CreateXXX-Methods. You can then add the nodes you
created like this to any other XmlElement created from the same XmlDocument.
It does not matter if you use doc.AppendChild(product) before or after
adding the child-Elements of <product />, the code could also look like
this:

XmlDocument doc = new XmlDocument();
XmlElement product = doc.CreateElement("Product");
doc.AppendChild(product);
XmlElement discount = doc.CreateElement("Discount");
product.AppendChild(discount);
XmlElement qtylow = doc.CreateElement("QtyLow");
discount.AppendChild(qtylow);
qtylow.AppendChild( doc.CreateTextNode("1") );
--
Pascal Schmitt
Nov 12 '05 #2
Thanks Pascal! It's easy to see now!!

Best
Harry

"Pascal Schmitt" <ne*******@cebra.nu> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
Hello!

But I'm drawing a blank as how to code this. Can anyone steer me in the
right direction?


XmlDocument doc = new XmlDocument();
XmlElement product = doc.CreateElement("Product");
XmlElement discount = doc.CreateElement("Discount");
XmlElement qtylow = doc.CreateElement("QtyLow");
qtylow.AppendChild( doc.CreateTextNode("1") );
discount.AppendChild(qtylow);
product.AppendChild(discount);
doc.AppendChild(product);
All Elements, regardless where they are added into the tree are created
using the XmlDocument.CreateXXX-Methods. You can then add the nodes you
created like this to any other XmlElement created from the same
XmlDocument.
It does not matter if you use doc.AppendChild(product) before or after
adding the child-Elements of <product />, the code could also look like
this:

XmlDocument doc = new XmlDocument();
XmlElement product = doc.CreateElement("Product");
doc.AppendChild(product);
XmlElement discount = doc.CreateElement("Discount");
product.AppendChild(discount);
XmlElement qtylow = doc.CreateElement("QtyLow");
discount.AppendChild(qtylow);
qtylow.AppendChild( doc.CreateTextNode("1") );
--
Pascal Schmitt

Nov 12 '05 #3

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

Similar topics

5
by: Tony Johansson | last post by:
Hello! I'm reading in a book about C++ and that is something that sound strange. It says "Pointers have reference-assignment semantics similar to those in Java. For example, after the...
4
by: fperfect13 | last post by:
Hi, I wanted to perform a deep copy of an array. Searching on google I ran into different opinions : C# Interview Questions (http://blogs.wwwcoder.com/tsvmadhav/archive/2005/04/08/2882.aspx)...
1
by: oDDskOOL | last post by:
I realized today that the Hashtable.Clone only produces a shallow copy... that makes me go mad that M$ doesn't even provide a deep copy ctor for the Hashtable class ! mighty tech ducks might...
4
by: Dennis | last post by:
I have several Data Structures, say "mystruct" which contain arrays of bytes, other structures, etc. I then dimension a variable (var1) as "mystruct" and set the various elements var1 to data. I...
6
by: Desmond Cassidy | last post by:
Hi, I'm sure this has been asked several times before but I'll risk it ;-) If I wish to save an Arraylist to another Arraylist and work on te original without affecting the contents of the new...
5
by: BenW | last post by:
Hello, What is the easiest way to make "deep copy" of my Hashtable? How about with other Collection classes in C#, any documents available? I don'r actually understand why Framework's...
2
by: bonk | last post by:
I have come across the need to distinguish between the creation of a deep and a shallow copy and with great interest I have read this article: ...
13
by: blangela | last post by:
I have decided (see earlier post) to paste my Word doc here so that it will be simpler for people to provide feedback (by directly inserting their comments in the post). I will post it in 3 parts...
4
by: shuisheng | last post by:
Dear All, Is there any easy way to make sure all my object copies are deep copy or shallow copy? I do not like to implement it in each class one by one. Thanks, Shuisheng
3
by: raylopez99 | last post by:
The "C# Cookbook" (O'Reilly / Jay Hilyard), section 3.26, is on deep cloning versus shallow cloning. The scanned pages of this book are found here: http://www.sendspace.com/file/mjyocg (Word...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.