delete from xml file using linq

Posted by Klbaiju under Regular Expressions on 11/30/2009 | Views : 2790 | Status : [Member] | Replies : 8
Hi i want to delete one record from xml file

this is the code

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


// persons.Remove();

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

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

this is working
but i want to change
where person.Element("id").Value == "100"
in to
where person.Element("id").Value = convert.toint32(textbox1.text)
i tried but showing errors
any solution

K L BAIJU




Responses

Posted by: vinodkus-5813 on: 1/10/2012 [Member] Starter | Points: 25

Up
0
Down
First of all,
You solved my problem to delete element in xml
Now I am solving your problem
you just write like this
where person.Element("id").Value = textbox1.text

Here person.Element("id").Value and
textbox1.text should be of same type and here both are string so no need to convert it into int32


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

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

Up
0
Down
What Error are you getting ?

Thank you for posting at Dotnetfunda
[Administrator]

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

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

Up
0
Down
iam getting string to int conversion error


K L BAIJU

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

Posted by: Abhi2434 on: 12/2/2009 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
convert.toint32(textbox1.text)
will through error if it cant parse the value in Textbox to Integer.

Are you sure you have proper value in textbox1.text ?

It is better to use

if(int.TryParse(textbox1.text, out result))


before doing this.


www.abhisheksur.com

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

Posted by: Vipinpaliwal on: 12/3/2009 [Member] Starter

Up
0
Down
Hi ,

Please Check this line
where person.Element("id").Value = convert.toint32(textbox1.text)

u wright this
where person.Element("id").Value == convert.toint32(textbox1.text)

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

Posted by: Johnvarenda on: 1/3/2010 [Member] Starter

Up
0
Down
hi....
I too getting the same problem but each of your answers worked good.
Can you give me the answer for this question...
Can we map an ASCII File to a predefined XSD Format. if yes, how?
Thanks...
.............
[url=http://www.e-datapro.net]data entry india[/url]

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

Posted by: Abhi2434 on: 1/4/2010 [Member] [Microsoft_MVP] [MVP] Silver

Up
0
Down
If the ASCII file you are talking about contains data in the same format as the XSD schema, you can do that.

Or please clarify what exactly you are looking for.

www.abhisheksur.com

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

Posted by: Klbaiju on: 1/4/2010 [Member] Starter

Up
0
Down
which answer do you really want

K L BAIJU

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

Login to post response