Monday, September 12, 2011

update xml file in asp.net

<?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"));
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; 

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")); 




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")); 


No comments :

Post a Comment