Small XML Data File needs updated.

  • Thread starter Thread starter Zanthor
  • Start date Start date
Z

Zanthor

I've written a small program that reads in content from an XML file
that looks like the following:

<?xml version="1.0"?>
<Inputs>
<Value Active = "True" Name="The name" Data="The data" />
(12 total of these nodes)
<Value Active = "True" Name="The Next name" Data="The data" />
</Inputs>

So the program loads this easily enough, but the part I'm not sure how
to go about this with is updating the information in this file when
the user changes the data or name attributes for a specific node.

Is there a way to specifically update the X'th node of this XML file?

Is there a better way to store/retrieve this data?

Other programs I've used registry entries to store information like
this, however this program needs to be portable in the fact that it
runs from a Thumb Drive and cannot store info on the local machine.

Thanks,

Will Dobbins
 
Hi will
this depends on where do you read your xml data ... do you read it in an
dataset object or an xmldocument object , but i think mostly you would
might need to rewite the whole file back from memory ... if you give more
details please do ....
 
Right now I'm reading the data using the XmlTextReader to pull the
values into my data array. I was thinking that using the DataSet would
probably be a good option, however since I'm either too new to this, or
too braindead to figure it out, it's been defeating me...

From what I read about DataSets I should be able to use one of those to
both read and write the document back out, either way it appears that
it's writing the entire document each time, which for the filesize
(2-3K) shouldn't be a major performance issue.

Perhaps any good references for DataSets for Noobs would help.

Thanks for replying,

Will Dobbins
 
Just an update for those who are following...

I scrapped the original structure and replaced it quite nicely with just
the DataSet features.

To read the data in from the XML:

FileStream fs = new FileStream("inputs.xml", FileMode.Open);
dsInputs.ReadXml(fs);
fs.Close();

And to write it back out... just as simple:

FileStream fs = new FileStream("inputs.xml", FileMode.Create);
dsInputs.WriteXml(fs);
fs.Close();

Hope that helps out anyone who's having similar issues in the future,
the DataSet object is impressively flexible!

Will Dobbins
 
Hi
look , i think it is better in your case to work with dataset ,,,, i will
give you a simple example ,and if you have any comments i will be
looking for your posts ............
lets say we have this simple xml file on the root c , the file name is
trail.xml .. and it look this way


<?xml version="1.0" standalone="yes"?>
<testfile>
- <person>
<name>tom</name>
<age>30</age>
<sex>male</sex>
</person>
- <person>
<name>lara</name>
<age>25</age>
<sex>female</sex>
</person>
</testfile>


you want to edit this xml data ,,,, this is how you go about it using
datasets


DataSet mine = new DataSet();
mine.ReadXml("c:\\trail.xml");
// once you do that you have loaded all your xml file into the dataset
// now it will creata table object named person and add it to the tables
collections of the dataset
// this table has columns that are named after children elments of he
person in your xml file
// so it has columns name , age , and sex
//and it has row that represent all your data


//you can do what you want with dataset elemnt now add edit modifiy
loopthrough...etc


// for example i will loop now for all elements looking for the name
lara , once i find that i will change the age to 27

for(int i=0;i<mine.Tables["person"].Rows.Count;i++) // means i will loop
all the rows of person
{
if (mine.Tables["person"].Rows["name"].ToString().Equals("lara"))//
test every row till you find which one is for lara
{
mine.Tables["person"].Rows["age"]= "280";//once you get a hit ,
change the age to 28

}
}

//to add a new row , you have to first create a new row object and add
it to the desired table
//into your dataset
DataRow k = mine.Tables["person"].NewRow();//create a new row that match
the structure of row in the table person
//start to add data to the row
k["name"] = "someone";
k["age"] = "29";
k["sex"]="male";
//then add the row to the table
mine.Tables["person"].Rows.Add(k);
//once you are done modifing the xml data all the way you want , you
simply write it back the file
// this way
mine.WriteXml("c:\\trail.xml");//write the modified dataset back to the
xml file




/// after running this code ... the xml file will be this
<?xml version="1.0" standalone="yes" ?>
- <testfile>
- <person>
<name>tom</name>
<age>30</age>
<sex>male</sex>
</person>
- <person>
<name>lara</name>
<age>280</age>
<sex>female</sex>
</person>
- <person>
<name>someone</name>
<age>29</age>
<sex>male</sex>
</person>
</testfile>
and if you want to delete from the xml file . you will just look for the
row that you want to delete , get is index , the use the delete method on
it ..........
 
Back
Top