XmlDocument - Write problems & How to Add Indentation!

G

Guest

Hi there,

I have 2 questions to ask.

1) I have this code...

XmlDocument doc = new XmlDocument();
doc.Load(myXmlPath);

int maxValue = 0;
int newValue = 0;

XmlNodeList nodeList = doc.SelectNodes("//Product");

foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToInt32(node.Attributes["id"].Value);

XmlElement newElem = doc.CreateElement("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttribute("id");
newAttr.Value = newValue.ToString();
newElem.Attributes.Append(newAttr);

doc.DocumentElement.AppendChild(newElem);
XmlElement elem = doc.CreateElement("Description");
elem.InnerText = "JVC Camera";
doc.DocumentElement.AppendChild(elem);

XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8);
doc.WriteTo(wrtr);
wrtr.Close();

Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panasonic Camcorder</Description>
</Product>
</ProductList>

Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JVC Camera</Description>
</ProductList>

Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JVC 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"?><ProductList><Product
id="1"><Description>Panasonic Camcorder</Description></Product><Product
id="2" /><Description>JVC Camera</Description></ProductList>

I hope i can indent nicely, just like what i did above on original xml file.

Any help please?
 
A

Alan

Hi,
1)
You created two element with same level, so you got that result.
The line "doc.DocumentElement.AppendChild(elem); " should be
"newElem.AppendChild(elem);"

2)
You could apply to Formatting.Indented the Formatting property of
XmlTextWriter.

Alan
 
G

Guest

Thanks Alan.

It works.

Cheers.

Alan said:
Hi,
1)
You created two element with same level, so you got that result.
The line "doc.DocumentElement.AppendChild(elem); " should be
"newElem.AppendChild(elem);"

2)
You could apply to Formatting.Indented the Formatting property of
XmlTextWriter.

Alan

Chua Wen Ching said:
Hi there,

I have 2 questions to ask.

1) I have this code...

XmlDocument doc = new XmlDocument();
doc.Load(myXmlPath);

int maxValue = 0;
int newValue = 0;

XmlNodeList nodeList = doc.SelectNodes("//Product");

foreach (XmlNode node in nodeList)
maxValue = XmlConvert.ToInt32(node.Attributes["id"].Value);

XmlElement newElem = doc.CreateElement("Product");
newValue = maxValue + 1;
XmlAttribute newAttr = doc.CreateAttribute("id");
newAttr.Value = newValue.ToString();
newElem.Attributes.Append(newAttr);

doc.DocumentElement.AppendChild(newElem);
XmlElement elem = doc.CreateElement("Description");
elem.InnerText = "JVC Camera";
doc.DocumentElement.AppendChild(elem);

XmlTextWriter wrtr = new XmlTextWriter(myXmlPath, Encoding.UTF8);
doc.WriteTo(wrtr);
wrtr.Close();

Xml (Before) // xml has identation nicely
----------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panasonic Camcorder</Description>
</Product>
</ProductList>

Xml(After) // xml has no identation
--------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panasonic Camcorder</Description>
</Product>
<Product id="2" />
<Description>JVC Camera</Description>
</ProductList>

Xml (Expected this)
-----------------------
<?xml version="1.0" encoding="utf-8"?>
<ProductList>
<Product id="1">
<Description>Panosonic Camcorder</Description>
</Product>
<Product id="2">
<Description>JVC 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"?><ProductList><Product
id="1"><Description>Panasonic Camcorder</Description></Product><Product
id="2" /><Description>JVC Camera</Description></ProductList>

I hope i can indent nicely, just like what i did above on original xml file.

Any help please?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top