Hi there,
I have 2 questions to ask.
1) I have this code...
XmlDocument doc = new XmlDocument();
doc.Load(myXmlP ath);
int maxValue = 0;
int newValue = 0;
XmlNodeList nodeList = doc.SelectNodes ("//Product");
foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToIn t32(node.Attrib utes["id"].Value);
XmlElement newElem = doc.CreateEleme nt("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttri bute("id");
newAttr.Value = newValue.ToStri ng();
newElem.Attribu tes.Append(newA ttr);
doc.DocumentEle ment.AppendChil d(newElem);
XmlElement elem = doc.CreateEleme nt("Description ");
elem.InnerText = "JVC Camera";
doc.DocumentEle ment.AppendChil d(elem);
XmlTextWriter wrtr = new XmlTextWriter(m yXmlPath, Encoding.UTF8);
doc.WriteTo(wrt r);
wrtr.Close();
Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
</ProductList>
Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JV C Camera</Description>
</ProductList>
Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Pa nosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JV C Camera</Description>
</Product>
</ProductList>
Note: I want the Second Description tag to be part of Product id="2", but
with my C# codes, i can't achieve it.
2)
After i perform the C# code on top, my codes has no more identation. It
seems that it removes all the whitespaces to this. (Please take note, the
above xml examples, i manually re-arrange the generated xml for clear view).
Xml (After)
--------------
<?xml version="1.0" encoding="utf-8"?><ProductLis t><Product
id="1"><Descrip tion>Panasonic Camcorder</Description></Product><Produc t
id="2" /><Description>J VC Camera</Description></ProductList>
I hope i can indent nicely, just like what i did above on original xml file.
Any help please?
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com