Appending a node to an existing xml file(C#)

7 views
Skip to first unread message

dexter

unread,
Apr 12, 2008, 11:40:46 PM4/12/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
Hi guys,
- I want to create a node in an existing xml file. For example, let's
assume there is an xml file called "songs.xml".
It looks like-
<?xml version="1.0"?>
<Songs>
<song><id>1</id>
<title>Till I die</title></song>
<song><id>2</id>
<title>Please forgive me</title>
</song>
</Songs>
.................................................................................................................
Now if I want another "song" node with id=3, how should I do that?
Please help.
Thanks in advance.

Rookie

unread,
Apr 15, 2008, 12:32:22 AM4/15/08
to DotNetDe...@googlegroups.com

   protected void btn_ins_Click(object sender, EventArgs e)
    {      
        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"));

        FillGrid();
    }

--
You can't make someone love you, all you can do is be someone who can be loved, the rest is up to the person to realize your worth.

CK

unread,
Apr 14, 2008, 1:37:52 PM4/14/08
to DotNetDevelopment, VB.NET, C# .NET, ADO.NET, ASP.NET, XML, XML Web Services,.NET Remoting
This is exactly the same as this thread:

http://groups.google.co.uk/group/DotNetDevelopment/browse_thread/thread/b8033efb40d28482/96d272e98ace3799

Alays read some threads before asking questions....
Reply all
Reply to author
Forward
0 new messages