Learn how to write over the data contained within xml file

  • Thread starter Thread starter Pauly Man
  • Start date Start date
  • Tags Tags
    Data File xml
AI Thread Summary
The discussion centers around challenges faced when working with XML in VB.NET, particularly in reading and writing XML files. Users express frustration with the lack of clear documentation and examples for handling XML, especially when compared to simpler formats like .ini files. While some have managed to read XML files, they struggle with updating specific regions of an XML document without rewriting the entire file. Key points include the importance of XML for separating data from applications, its widespread use in modern software, and the need for reliable resources to understand how to create legal XML files and manipulate them programmatically in VB.NET. Examples provided demonstrate how to use the XmlSerializer, DataSet, and XmlDocument classes to read and write XML data effectively. These methods allow for loading data into custom classes or datasets, modifying it, and saving changes back to XML files.
Pauly Man
Messages
129
Reaction score
0
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.
 
Computer science news on Phys.org
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 separate 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:
Sorry if 'Profile Badge' is not the correct term. I have an MS 365 subscription and I've noticed on my Word documents the small circle with my initials in it is sometimes different in colour document to document (it's the circle at the top right of the doc, that, when you hover over it it tells you you're signed in; if you click on it you get a bit more info). Last night I had four docs with a red circle, one with blue. When I closed the blue and opened it again it was red. Today I have 3...
Thread 'ChatGPT Examples, Good and Bad'
I've been experimenting with ChatGPT. Some results are good, some very very bad. I think examples can help expose the properties of this AI. Maybe you can post some of your favorite examples and tell us what they reveal about the properties of this AI. (I had problems with copy/paste of text and formatting, so I'm posting my examples as screen shots. That is a promising start. :smile: But then I provided values V=1, R1=1, R2=2, R3=3 and asked for the value of I. At first, it said...
Back
Top