KAUSHIK
KAUSHIK

Reputation: 21

How to read, store and delete xml data in C#

<SMSWall run="yes" nonstop="False">
    <starttime>10:15:25 PM</starttime>
    <endtime>10:15:25 PM</endtime>
</SMSWall>
<MediaPlayer run="yes" nonstop="False">
    <starttime>10:15:25 PM</starttime>
    <endtime>10:15:25 PM</endtime>
</MediaPlayer>
<Bidding run="yes" nonstop="False">
    <starttime>10:15:25 PM</starttime>
    <endtime>10:15:25 PM</endtime>
</Bidding>

This is my xml file. Now I want to read the xml, store the value in a variable (and also want the data to read from the string), and leave the .xml file so that it can be deleted.I also want to read the value node wise like:

XmlDocument document = new XmlDocument();
document.Load("Schedule.xml");
XmlNode ht=document.SelectSingleNode("SMSWall/@run");

Upvotes: 2

Views: 868

Answers (2)

melmi
melmi

Reputation: 1008

I prefer to create a DataSet and then use it like this:

DataSet1 ds = new DataSet1();
ds.ReadXml("test.xml", XmlReadMode.ReadSchema);
//do something
ds.WriteXml("test.xml", XmlReadMode.ReadSchema);

A typed DataSet will provide you typed tables and rows. You can get rows by index or query. You can find more about the here.

Upvotes: 0

Saeed Amiri
Saeed Amiri

Reputation: 22555

Your current XML file is not in correct format (i.e save it as .xml and open it with IE, you will give an error). So you should add <root> ... </root> (or other name). In all I think best way to treat with XML is using System.Xml.Linq library, like XDocument and XElement :

Loading data: XDocument.Load(filename);
Creating item: XElement root = new XElement("Root");
Searching: document.Descendants("Root"), ...

See MSDN for other samples.

Upvotes: 1

Related Questions