hi, i am wondering if anyone has a code snippet or example to allow me to read the "Style Name attribute" and child elements to a textbox, and possible write back to file from textboxes to the XML example below?
thanks for any help.
XML Example:
2 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
The following example shows how you can read data from an existing XML file and then write out updated data to another file (or overwrite the first one if you're brave). Sorry, it's in C# as I haven't had time to translate it into VB.Net but I'm sure you'll be able to follow it OK:
JayPosted Mar 19, 2008, 1:17 AM
AlanPosted Mar 2, 2008, 12:54 PM
The following example shows how you can read data from an existing XML file and then write out updated data to another file (or overwrite the first one if you're brave). Sorry, it's in C# as I haven't had time to translate it into VB.Net but I'm sure you'll be able to follow it OK:
using System;
using System.Xml;
class Test
{
static void Main()
{
using (XmlTextReader reader = new XmlTextReader("jay.xml"))
{
reader.ReadToFollowing("Style");
string name = reader.GetAttribute("name");
Console.WriteLine(name);
reader.ReadToFollowing("File");
string backFile = reader.ReadElementString("File");
Console.WriteLine(backFile);
string stretched = reader.ReadElementString("Stretched");
Console.WriteLine(stretched);
string cropped = reader.ReadElementString("Cropped");
Console.WriteLine(cropped);
reader.ReadToFollowing("File");
string foreFile = reader.ReadElementString("File");
Console.WriteLine(foreFile);
string transparency = reader.ReadElementString("Transparency");
Console.WriteLine(transparency);
string enabled = reader.ReadElementString("Enabled");
Console.WriteLine(enabled);
}
using (XmlTextWriter writer = new XmlTextWriter("jay2.xml", null))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartElement("ExampleMain");
writer.WriteStartElement("Settings");
writer.WriteStartElement("Style");
writer.WriteStartAttribute("name");
writer.WriteString("test124");
writer.WriteEndAttribute();
writer.WriteStartElement("Background");
writer.WriteStartElement("File");
writer.WriteString("image03.bmp");
writer.WriteEndElement();
writer.WriteStartElement("Stretched");
writer.WriteString("1");
writer.WriteEndElement();
writer.WriteStartElement("Cropped");
writer.WriteString("0");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteStartElement("Foreground");
writer.WriteStartElement("File");
writer.WriteString("image6.bmp");
writer.WriteEndElement();
writer.WriteStartElement("Transparency");
writer.WriteString("32");
writer.WriteEndElement();
writer.WriteStartElement("Enabled");
writer.WriteString("0");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
Console.ReadLine();
}
}