Learn how to write over the data contained within xml file

In summary, xml is awesome for separating raw data from an application, and allows you to completely separate an application from the data it uses. You can load the data into custom classes, or load it into an XmlDocument. There is a lot of useful and reliable information out there on how to use xml, but it is all behind the scenes. I hope this information was helpful.
  • #1
Pauly Man
129
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
  • #2
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:
  • #3


Hi there,

Sorry to hear that you are having trouble with xml. It can definitely be overwhelming at first, but with some practice and the right resources, you will be able to successfully work with xml files.

To write over data contained within an xml file, you will need to use an xml parser or library in your chosen programming language. In vb.net, you can use the XmlTextReader class or the XmlDocument class to read and manipulate xml files.

To update data within an xml file, you will need to first locate the specific element or attribute that you want to update. You can do this by using methods like SelectSingleNode or SelectNodes to query the xml document. Once you have located the element or attribute, you can use the appropriate methods to update its value.

As for creating a new xml file programmatically, you can use the XmlDocument class to create a new xml document and then use methods like CreateElement and AppendChild to add elements and attributes to the document.

As for reliable resources, I would recommend checking out the official Microsoft documentation on using xml in vb.net. They have detailed explanations and examples that can guide you through the process. You can also check out online tutorials and forums for additional support.

I hope this helps and good luck with your xml endeavors!
 

FAQ: Learn how to write over the data contained within xml file

What is XML?

XML stands for Extensible Markup Language and is a popular format for storing and sharing data on the internet. It uses tags to structure and organize data, making it easily readable and transferable between different systems.

Why is it important to learn how to write over data within an XML file?

Being able to write over data within an XML file allows you to update and modify the information contained within it. This is especially useful in situations where data is constantly changing or needs to be updated frequently.

How do you write over data within an XML file?

To write over data within an XML file, you will need to use an XML editor or a programming language that supports XML. You can use the appropriate syntax to select and modify the data within the file, and then save the changes.

Can you give an example of writing over data within an XML file?

Sure, let's say you have an XML file containing information about different books. You can use the appropriate syntax to select and modify the title of a specific book, and then save the changes to the file.

Are there any best practices to keep in mind when writing over data within an XML file?

Yes, it is important to make sure that the changes you make do not break the structure of the XML file. It is also recommended to keep backups of the original file in case any errors occur during the writing process.

Similar threads

Replies
13
Views
1K
Replies
2
Views
2K
Replies
22
Views
2K
Replies
2
Views
1K
Replies
1
Views
1K
Replies
10
Views
1K
Back
Top