Modify XML Data using xPathDocument.

122 views
Skip to first unread message

Shailendrasinh Zala

unread,
Sep 21, 2007, 12:03:45 AM9/21/07
to DotNetDe...@googlegroups.com
How can I Modify,Add and Delete XML Data using xPathDocument in C#.Net?
I want to add attribute and tag and child node.

Please Help me
its urgent.


Cerebrus

unread,
Sep 21, 2007, 12:08:51 AM9/21/07
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Use the XPathDocument.CreateNavigator to get an XPathNavigator object
that will allow you to move anywhere within the document. Then use the
Navigator's SetValue or any of the Append/Delete methods to modify all
you want.

bixam.palsa

unread,
Sep 21, 2007, 1:33:48 AM9/21/07
to DotNetDe...@googlegroups.com
using System.Xml;
using System.Xml.XPath;

Insertion:
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(MapPath("~/App_Data/Users.xml"));
        XmlDocumentFragment docFrag = xmlDoc.CreateDocumentFragment();
        string id = txt_ins_uid.Text;
        string name = txt_ins_uname.Text;
        string pwd = txt_ins_pwd.Text;
        docFrag.InnerXml = "<user><id>"+id+"</id><name>"+name+"</name><password>"+pwd+"</password></user>";
        XmlNode childNode = xmlDoc.DocumentElement ;
        childNode.InsertAfter(docFrag, childNode.LastChild);
        xmlDoc.Save(MapPath("~/App_Data/Users.xml"));
Updattion:
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(MapPath("~/App_Data/Users.xml"));
        XmlNode name = xmlDoc.SelectSingleNode("/UsersList/user[id='" + txt_upd_uid.Text + "']/name");
        name.InnerText = txt_upd_uname.Text;
        XmlNode pwd = xmlDoc.SelectSingleNode("/UsersList/user[id='" + txt_upd_uid.Text + "']/password");
        pwd.InnerText = txt_upd_pwd.Text;
        xmlDoc.Save (MapPath("~/App_Data/Users.xml"));
Deletion:
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(MapPath("~/App_Data/Users.xml"));
        XmlNode root = xmlDoc.DocumentElement;
        XmlNode childNode = xmlDoc.SelectSingleNode("UsersList/user[id='" + txt_del_uid.Text + "']");
        root.RemoveChild(childNode);
        xmlDoc.Save(MapPath("~/App_Data/Users.xml"));
Users.xml:
<?xml version="1.0" encoding="utf-8"?>
<UsersList>
  <user>
    <id>100</id>
    <name>Suresh</name>
    <password>Kothapally</password>
  </user> 
</UsersList>
--
Problem is common to all but attitude makes the difference.
Message has been deleted
Message has been deleted

Shailendrasinh Zala

unread,
Sep 21, 2007, 2:17:36 AM9/21/07
to DotNetDe...@googlegroups.com
Thanks
it is working perfectly.
you are great.

Alex Itelman

unread,
Sep 21, 2007, 8:38:51 AM9/21/07
to DotNetDe...@googlegroups.com

Please visit: http://alexitelman.110mb.com/index.htm

I've created a new forum there, you can post your topics there.

 

 

---

With Regards,

    Alex Itelman

    (972) 54 52 00 956

    (972) 57 81 14 712

Shailendrasinh Zala

unread,
Sep 21, 2007, 12:27:07 AM9/21/07
to DotNetDe...@googlegroups.com
I try do it with this code but still i found error.
= xmlPathNav.AppendChild("DataXml");=
but it generate error.

have you any solution.

On 9/21/07, Shailendrasinh Zala <syog...@gmail.com> wrote:
can you give me any example?

Shailendrasinh Zala

unread,
Sep 21, 2007, 12:14:35 AM9/21/07
to DotNetDe...@googlegroups.com
can you give me any example?


On 9/21/07, Cerebrus <zor...@sify.com> wrote:
Reply all
Reply to author
Forward
0 new messages