update xml file in asp.net

Posted by Klbaiju under Regular Expressions on 10/6/2009 | Views : 6299 | Status : [Member] | Replies : 4
Hi,
this is my xml file

<?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person>
<id>100</id>
<Name>Ella</Name>
<City>LA</City>
<Age>13</Age>
</Person>
<Person>
<id>101</id>
<Name>Ingrid</Name>
<City>Oslo</City>
<Age>63</Age>
</Person>
<Person>
<id>102</id>
<Name>baiju</Name>
<City>kollam</City>
<Age>37</Age>
</Person>
<Person>
<id>103</id>
<Name>dd</Name>
<City>hhh</City>
<Age>888</Age>
</Person>
<Person>
<id>104</id>
<Name>ee</Name>
<City>eee</City>
<Age>543</Age>
</Person>
</Persons><?xml version="1.0" encoding="utf-8"?>
<Persons>
<Person>
<id>100</id>
<Name>Ella</Name>
<City>LA</City>
<Age>13</Age>
</Person>
<Person>
<id>101</id>
<Name>Ingrid</Name>
<City>Oslo</City>
<Age>63</Age>
</Person>
<Person>
<id>102</id>
<Name>baiju</Name>
<City>kollam</City>
<Age>37</Age>
</Person>
<Person>
<id>103</id>
<Name>dd</Name>
<City>hhh</City>
<Age>888</Age>
</Person>
<Person>
<id>104</id>
<Name>ee</Name>
<City>eee</City>
<Age>543</Age>
</Person>
</Persons>

i want to update this file

i try following code
but showing erors
Error is
object reference not to set instance of an object


XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Server.MapPath("people.xml"));

XmlNodeList nodeList = xmlDoc.SelectNodes("persons/person[@ID='"+txttid.Text+"']");


nodeList[0].ChildNodes[1].InnerText = txtName.Text;

nodeList[0].ChildNodes[2].InnerText = txtCity.Text;

nodeList[0].ChildNodes[3].InnerText = txtAge.Text;



xmlDoc.Save(Server.MapPath("people.xml"));
how it is possible

Regards
K L BAIJU




Responses

Posted by: Thilaga on: 10/8/2009 [Member] Bronze

Up
0
Down
hi,

XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Server.MapPath("People.xml"));

XmlNodeList nodeList = xmlDoc.SelectNodes("//Persons//Person[id='" + txttid.Text + "']");

nodeList[0].ChildNodes[1].InnerText = txtName.Text;

nodeList[0].ChildNodes[2].InnerText = txtCity.Text;

nodeList[0].ChildNodes[3].InnerText = txtAge.Text;

xmlDoc.Save(Server.MapPath("People.xml"));


This will work. plz try.

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 10/8/2009 [Member] Starter

Up
0
Down
that is working now
thanks
do you know how to write
query in xml

i want to display the record where id =100
how it is possible


Regards
K L Baiju

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thilaga on: 10/8/2009 [Member] Bronze

Up
0
Down
XmlDocument xmlDoc = new XmlDocument();

xmlDoc.Load(Server.MapPath("People.xml"));

XmlNode nodeList = xmlDoc.SelectSingleNodes("//Persons//Person[id='" + txttid.Text + "']");

txtName.Text=nodeList.ChildNodes[1].InnerText;

txtCity.TextnodeList.ChildNodes[2].InnerText;

txtAge.TextnodeList.ChildNodes[3].InnerText;

Try this. instead of writing query this will work.

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Klbaiju on: 10/9/2009 [Member] Starter

Up
0
Down
yes it is working

how it will display in a gridview

Klbaiju, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response