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?
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?