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
Click For Summary
SUMMARY

This discussion focuses on the challenges of reading and writing XML files using VB.NET. The user initially struggled with API functions for .ini files and found XML to be a more viable option despite difficulties in understanding its structure. Key solutions provided include using the XmlSerializer, DataSet, and XmlDocument classes to read and update XML data programmatically. The conversation emphasizes the need for clear examples and reliable resources to effectively manipulate XML in VB.NET.

PREREQUISITES
  • Understanding of XML file structure and syntax
  • Familiarity with VB.NET programming language
  • Knowledge of .NET Framework classes such as XmlSerializer, DataSet, and XmlDocument
  • Basic understanding of object-oriented programming concepts
NEXT STEPS
  • Research how to create a legal XML file in VB.NET
  • Learn to programmatically write a new XML file using XmlSerializer in VB.NET
  • Explore methods to update specific regions of an existing XML file using XmlDocument in VB.NET
  • Investigate best practices for error handling when working with XML in .NET applications
USEFUL FOR

VB.NET developers, software engineers working with XML data, and anyone seeking to enhance their skills in XML manipulation within .NET applications.

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:

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
Replies
7
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K