Learn how to write over the data contained within xml file

  • Thread starter Pauly Man
  • Start date
Hi guys.

I'm having real trouble with xml. Ideally I'd like to simply play around with .ini files, but the api functions don't seem to work anymore with vb.net. Well not quite true, the api's to write to an .ini file work, but the api's for reading in data just return gibberish for me. I looked around for help and everywhere I look it tells me to use xml instead. After a week of searching every nook and cranny of the internet I have finally worked out how to read an xml file. However NO website I have found can tell me exactly how the hell i should set up my xml so that it is legal, and works fine. I tried making an xml schema in vb.net but that just caused countless extra problems for me when trying to access the data, so i've given up on that, I've instead made a real simple xml fragment, and the program I'm making can read in the data.

Now I want to learn how to write over the data contained within my xml file, not write over the entire file, just update it. I can't find consistent information on this anywhere, I am at the point where I just can't see what all the fuss is about xml, I can't make it work and its so much trouble at the moment.

Any help would be greatly appreciated. Maybe pointers to USEFUL and RELIABLE information on how to write a legal xml file in vb.net, how to write a new xml file programmatically from vb.net, and if possible how to write over regions of an existing xml file programmatically in vb.net.
 
I am at the point where I just can't see what all the fuss is about xml
xml is awesome! It allows you to completely seperate raw data from an application. Once an xml document is made any system can be made to use that information. You'd be surprised how many programs today use xml. You don't realize it because it's all behind the scenes.

Any help would be greatly appreciated. Maybe pointers to USEFUL and RELIABLE information on how to write a legal xml file in vb.net, how to write a new xml file programmatically from vb.net, and if possible how to write over regions of an existing xml file programmatically in vb.net.
I completely understand your frustration with xml, especially with .net, I was in your same boat. Everyone says how easy it is, but there doesn't seem to be solid examples and documentation around. I used xml in a C# app to populate a menu toolbar thingie. Essentially it is fairly easy. You can load the data into
custom classes using the XmlSerializer, or you can load it into a DataSet, or you can load it into an XmlDocument. Here is a bit of code that helped me figure it out:

Code:
Where the XmlSerializerTest does the following:

void SerializerTest()
{
XmlTextReader r = new XmlTextReader(args[0]);
XmlSerializer s = new XmlSerializer(typeof(Data));
Data data = (Data)s.Deserialize(r);
r.Close();

foreach (Table t in data.Tables)
{
t.dtDate = DateTime.Now;
}

XmlTextWriter w = new XmlTextWriter(args[1], Encoding.UTF8);
w.Formatting = Formatting.Indented;
s.Serialize(w, data);
}

where the Data and Table classes are defined as follows:

public class Data
{
public Data() {}

[XmlElement("Table",typeof(Table))]
public IList Tables = new ArrayList();
}

public class Table
{
public Table() {}
public int pkReportHourly;
public DateTime dtDate;
public int iVisits ;
public int iSignings;
public int iCheckouts;
public int iPurchases;
}

The DataSet does the following:

void DatasetTest()
{
DataSet ds = new DataSet("Data");
DataTable dt = new DataTable("Table");
dt.Columns.Add("pkReportHourly", typeof(int));
dt.Columns.Add("dtDate", typeof(DateTime));
dt.Columns.Add("iVisits", typeof(int));
dt.Columns.Add("iSignings", typeof(int));
dt.Columns.Add("iCheckouts", typeof(int));
dt.Columns.Add("iPurchases", typeof(int));
ds.Tables.Add(dt);

ds.ReadXml(args[0], XmlReadMode.IgnoreSchema);

foreach (DataRow r in dt.Rows)
{
r[1] = DateTime.Now;
}

ds.WriteXml(args[1], XmlWriteMode.IgnoreSchema);
}

And the XmlDocumentTest does the following:

void XmlDocumentTest()
{
XmlDocument doc = new XmlDocument();
doc.Load(args[0]);

foreach (XmlElement e in doc.SelectNodes("Data/Table/dtDate"))
{
e.InnerText = DateTime.Now.ToString("s");
}

doc.Save(args[1]);
}
 
Last edited:

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top