I store configuration in XML format. For now I edit them as text. So I format them in a easy to read format. Now I am going to add a tool to edit them. But sometime I still need edit them as text directly. So I wonder if there is a way I can edit some xml
content or property without touching other part. For example, one xml tag could contain Html, sometime, I write it like
<label><![CDATA[ this is a & test]]></label>
<quantity>10</quantity>
After modify the quantity programmically, I don;t want it change to
<label>this is a & test</label>
<quantity>10</quantity>
neither
<label><![CDATA[ this is a & test]]></label><quantity>10</quantity>
I am ok to anyway, just want to archieve what I said.
To Triggered, I think you didn't read my question, if simply load and change then save, XML engineer for sure need parse the whole xml and will save in its own way, but I want to keep the original format.
I am ok to anyway, just want to archieve what I said.
To Triggered, I think you didn't read my question, if simply load and change then save, XML engineer for sure need parse the whole xml and will save in its own way, but I want to keep the original format.
<root>
<
label><![CDATA[ this is a & test]]></label>
<quantity>10</quantity>
</
root>
you can acheive by using xmlwriter and xmlreader. Below is sample code. you can also use xmldocument instead of xmlwriter. Below code works for above xml.
The code runs well, the result is not right, it returns <?xml version="1.0" encoding="utf-16"?><root><label><![CDATA[ this is a & test]]><quantity>30</quantity></label></root>, but I know what I you mean.
But the problem is it changes the original format, what I want is a way to be able to modify the xml by both programmatically and manually. So to be able to do that, I want to keep whatever format is in there. For example, one user edit the xml manually
like this
<root>
<label><![CDATA[ this is a & test]]><label>
<quantity>30</quantity><quantity2>3</quantity2>
</root>
quantity is on the same line with quantity2, I want to keep it that way when I change it programmatically.
wyx2000
Contributor
3388 Points
873 Posts
How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 06:54 PM|LINK
I store configuration in XML format. For now I edit them as text. So I format them in a easy to read format. Now I am going to add a tool to edit them. But sometime I still need edit them as text directly. So I wonder if there is a way I can edit some xml content or property without touching other part. For example, one xml tag could contain Html, sometime, I write it like
<label><![CDATA[ this is a & test]]></label>
<quantity>10</quantity>
After modify the quantity programmically, I don;t want it change to
<label>this is a & test</label>
<quantity>10</quantity>
neither
<label><![CDATA[ this is a & test]]></label><quantity>10</quantity>
Any suggestion?
thanks
triggered
Contributor
3356 Points
908 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 07:27 PM|LINK
Yes,
Use XmlDocument.
XmlDocument xDoc = new XmlDocument();
load the file and use xDoc.SelectSingleNode("xpath") to get to the elemetn you want to edit.
Then xDoc.Save("file");
if this isnt a file, then instead of loading a file, load the xml string, and instead of saving write it back to wherever it needs to go.
kalvagadda
Participant
1513 Points
281 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 07:29 PM|LINK
Hi,
how are you modifying xml programatically..
you are using xmlwriter , xmldocument or xmltextwriter.
Thanks,
Kiran
wyx2000
Contributor
3388 Points
873 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 07:34 PM|LINK
I am ok to anyway, just want to archieve what I said.
To Triggered, I think you didn't read my question, if simply load and change then save, XML engineer for sure need parse the whole xml and will save in its own way, but I want to keep the original format.
triggered
Contributor
3356 Points
908 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 07:37 PM|LINK
" I wonder if there is a way I can edit some xml content or property with touching other part "
You need to edit your question, cause you asked with the other parts being touched, not "not being" touched.
Im out.
kalvagadda
Participant
1513 Points
281 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 08:12 PM|LINK
<
label><![CDATA[ this is a & test]]></label> <quantity>10</quantity></
root>you can acheive by using xmlwriter and xmlreader. Below is sample code. you can also use xmldocument instead of xmlwriter. Below code works for above xml.
StringBuilder sb = new StringBuilder(); XmlWriter xw = XmlWriter.Create(sb); XmlReader xr = XmlReader.Create(Server.MapPath("delete.xml"));while (xr.Read()){
switch (xr.NodeType){
case XmlNodeType.CDATA:xw.WriteCData(xr.ReadContentAsString());
break; case XmlNodeType.Element:xw.WriteStartElement(xr.Name);
if (xr.Name.Equals("quantity")){
xw.WriteValue("30");xr.Read();
xr.ReadContentAsString();
}
break;case XmlNodeType.EndElement:xw.WriteEndElement();
break; default:break;}
}
xr.Close();
xw.Flush();
xw.Close();
txtXml.Text = sb.ToString();
Thanks,
Kiran
wyx2000
Contributor
3388 Points
873 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 09:25 PM|LINK
Hi, thank you very much for your time
The code runs well, the result is not right, it returns <?xml version="1.0" encoding="utf-16"?><root><label><![CDATA[ this is a & test]]><quantity>30</quantity></label></root>, but I know what I you mean.
But the problem is it changes the original format, what I want is a way to be able to modify the xml by both programmatically and manually. So to be able to do that, I want to keep whatever format is in there. For example, one user edit the xml manually like this
<root>
<label><![CDATA[ this is a & test]]><label>
<quantity>30</quantity><quantity2>3</quantity2>
</root>
quantity is on the same line with quantity2, I want to keep it that way when I change it programmatically.
kalvagadda
Participant
1513 Points
281 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 03, 2007 10:17 PM|LINK
Hi,
<root><
label><![CDATA[ this is a & test]]></label> <quantity>20</quantity> <quantity2>3</quantity2> </root>to maintain format you can try following code. Below code works with above xml
StringBuilder sb = new StringBuilder(); XmlWriterSettings xws = new XmlWriterSettings(); xws.OmitXmlDeclaration = true; XmlWriter xw = XmlWriter.Create(sb); XmlReader xr = XmlReader.Create(Server.MapPath("delete.xml"));while (xr.Read()){
switch (xr.NodeType){
case XmlNodeType.CDATA:xw.WriteCData(xr.Value);
break; case XmlNodeType.Element:xw.WriteStartElement(xr.Name);
if (xr.Name.Equals("quantity")){
xw.WriteValue("30");xr.Read();
}
break;case XmlNodeType.EndElement:xw.WriteEndElement();
break; case XmlNodeType.Text:xw.WriteValue(xr.Value);
break;case XmlNodeType.Whitespace:xw.WriteWhitespace(xr.Value);
break; default:break;}
}
xr.Close();
xw.Flush();
xw.Close();
txtXml.Text = sb.ToString();
Thanks,
Kiran
wyx2000
Contributor
3388 Points
873 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 04, 2007 12:00 AM|LINK
nice, looks like I just need expand it a bit to handle all element and properties.
Thanks for you help!
wyx2000
Contributor
3388 Points
873 Posts
Re: How can I write xml with original content untouched and/or with human friendly format, thanks
Aug 04, 2007 12:15 AM|LINK
Hi, one more question.
How can I keep <field name="test"></field> as it is, rather than <field name="test"/> after transform?
thanks