update xml file using linq

Posted by Klbaiju under Regular Expressions on 12/3/2009 | Views : 2645 | Status : [Member] | Replies : 2
Hi iam trying to update a record in an xml file using linq.
but showing error
error is object reference not set to an instance of an object.
here is the code

XDocument xmlDoc = XDocument.Load(Server.MapPath("people.xml"));
var persons = from person in xmlDoc.Descendants("Person")
where person.Element("id").Value == (TextBox1.Text)
select person;


// persons.Remove();

foreach (XElement person in persons.ToList())
{

person.Element("name").Value = (TextBox2.Text);

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

Regards
K L BAIJU




Responses

Posted by: Vuyiswamb on: 12/4/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Which line are you getting this Error ?

Thank you for posting at Dotnetfunda
[Administrator]

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

Posted by: Klbaiju on: 12/4/2009 [Member] Starter

Up
0
Down
Hi i already solved it

this is final code
error was i wrote ("name") instead of ("Name")
.................
XDocument xmlDoc = XDocument.Load(Server.MapPath("people.xml"));
var persons = from person in xmlDoc.Descendants("Person")
where person.Element("id").Value == (TextBox1.Text)
select person;


// persons.Remove();

foreach (XElement person in persons.ToList())
{

person.Element("Name").Value = TextBox2.Text;
// person.SetElementValue("Name",TextBox2.Text);

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


}

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

Login to post response