Introduction: There are many ways to read, write and delete the xml tags but here I am using Xpath to read, write and delete the xml tags. It is very easy to read, write and delete the xml tags using X-path because we give the directions (we know where the exact tag of xml is to be changed). Coding: If you want to code using Xpath then first use following Namespaces: Using System.Xml.Xpath; Using System.Xml;
Here there are many different xml tags (with & without attributes)
Example 1: If the xml file is like the following xml file:
<?xml version="1.0" encoding="utf-8"?> <Settings> <appsettings value=”1”> <add key = “king” value = “1”/> </appsettings> <appsettings value=”2”> <add > text</add> </appsettings> <Bar> Dheeraj </Bar> </Settings>
Suppose you to read and modify the ⁢Bar> tags which contains the innerxml = “Dheeraj” and change the value from “Dheeraj” to “Kumar” then
XmlDocument doc = new XmlDocument(); doc.Load((@"c:/newfile.xml"); XmlNode xnode = doc.SelectSingleNode("//Settings/Bar"); //read the value of “Dheeraj”) String Str = xnode.Innerxml; //TO Update the value of “Dheeraj” to “Kumar” Xnode.innerxml = “kumar”; //TO delete the value of “Dheeraj” (all nodes of add tag) Xnode.removeall(); doc.save((@"c:/newfile.xml");
_____________________________________________________________________ Example 2:
If the xml file is like the following xml file:
<?xml version="1.0" encoding="utf-8"?> <Settings> <appsettings value=”1”> <add key = “king” value = “1”/> <appsettings> <appsettings value=”2”> <add > text</add> // ?change from “text” to”newtext” </appsettings> <Bar>Dheeraj</Bar> </Settings>
You want to change the value of “text” to “newtext”
XmlDocument doc = new XmlDocument(); doc.Load((@"c:/newfile.xml"); XmlNode xnode = doc.SelectSingleNode("//Settings/appsettings[@value=’2’]/add"); //read the value of “text”) String Str = xnode.InnerxmlXnode.innerxml = “newtext”; //TO Update the value of text” to “newtext” doc.save((@"c:/newfile.xml");
_________________________________________________________________
Example 3: If the xml file is like the following xml file: <?xml version="1.0" encoding="utf-8"?> <Settings> <header> <item> <parameter name = “DB” value = “data”/> // ?change from “data” to”newdata” <parameter name = “server” value = “new”/> </item> </header> <header> <item> <parameter name = “BB” value = “CC”/> <parameter name = “AA” value = “DD”/> </item> </header> </Settings>
You want to change the value of “data” to “newdata”
XmlDocument doc = new XmlDocument(); doc.Load((@"c:/newfile.xml"); XmlNode xnode = doc.SelectSingleNode("//Settings/header/item/parameter[@name=’ DB’]"); String Str = xnode.Attributes[1].value;//(contains the value of “data”) //TO Update the value of data” to “newdata” xnode.Attributes[1].value = “newdata”; doc.save((@"c:/newfile.xml");
_____________________________________________________________________ Example 4: when i am using data config file , there is a tag called <enterpriseLibrary.databaseSettings> .i was unable to read the child nodes which are present in <enterpriseLibrary.databaseSettings> tag. This is because of the fullstop present in the tag. so the only solution was to take the complete xml file in a string and then just write it in the xml file and save it. Suppose you have Xml file like this following file: <parameters.tags> <parameter name="data" value="exensys" isSensitive="false"/> <parameter name="server" value="loca" isSensitive="false"/>
because of the parameter.tags i was not able to read the child node "parameter". so what i did was :
String str = " <parameters.tags> <parameter name=\"data\" value=\"exensys\" isSensitive=\"false\"/> <parameter name=\"server\" value=\""+txtDB.Text+"\" isSensitive=\"false\"/>" XmlDocument doc = new XmlDocument(); doc.InnerXml = str; doc.save((@"c:/newfile.xml");
see in this string , i am just replace "loca" with the value of the textbox valus. if you have any suggestions please contact me. Thank you
|
No responses found. Be the first to respond and make money from revenue sharing program.
|