C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




READ, UPDATE & DELETE XML FILE USING X-PATH


Posted Date: 07 Feb 2007    Resource Type: Articles    Category: .NET Framework

Posted By: Gundra dheeraj       Member Level: Silver
Rating:     Points: 10



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




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
XPATH  .  XML  .  UPDATE  .  READ  .  FILE  .  DELETE  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Visual Studio 2005 SP1 - Features - What is there in it?
Previous Resource: OOPs In JavaScript
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use