-
Jun 23rd, 2010, 06:16 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] XML parsing efficiency
Hi,
I decided to put the settings for an application in an xml file.
The format is as follows
Code:
...
<settings>
<conf1>Value1</conf1>
<conf2>Value2</conf2>
<conf3 />
<conf4>Value4</conf4>
</settings>
What I did before was reading through all elements and check the element name with a switch each time (with an xmltextreader).
It works, but strikes to me as rather inefficient.
I've been experimenting with xmldocument but I'm not getting very far.
This is what I at the moment:
Code:
StreamReader sw = new StreamReader(Path, Encoding.Unicode);
XmlDocument xdoc = new XmlDocument();
xdoc.Load(sw);
XmlElement baseElement = xdoc.DocumentElement["settings"];
Any pointers?
Thanks
Delete it. They just clutter threads anyway.
-
Jun 24th, 2010, 03:59 AM
#2
Re: XML parsing efficiency
Hmm. .NET 2 - that means you cannot use LINQ which is a shame.
With the above method you would probably have to use a foreach loop to cycle over the element descendants. Here is something for you to read.
-
Jun 24th, 2010, 10:57 AM
#3
Lively Member
Re: XML parsing efficiency
Use SelectNodes() from the document with an XPath string and your life will become easy
I wrote a book Visual Studio 2008 Programming
Amazon.com / Visual Studio 2008 Programming / By Jamie Plenderleith & Steve Bunn
-
Jun 24th, 2010, 12:10 PM
#4
Re: XML parsing efficiency
I'd create a class, then use XML Serialization to get the settings in/out of it....then you don't even need to deal with XPath.
-tg
-
Jun 25th, 2010, 02:27 PM
#5
Re: XML parsing efficiency
Another quick and dirty method would be to utilize the DataTable's Read/Write XML methods:
Code:
private void WriteSettings()
{
DataTable settings = new DataTable("Settings");
settings.Columns.Add("Config1", typeof(string));
settings.Columns.Add("Config2", typeof(string));
// Etc..
settings.Rows.Add(new object[] {
"Value1",
"Value2",
// Etc..
});
settings.WriteXml(@"C:\Tests\Settings.xml", XmlWriteMode.WriteSchema);
}
private void ReadSettings()
{
DataTable settings = new DataTable();
settings.ReadXml(@"C:\Tests\Settings.xml");
Debug.Print(settings.Rows[0]["Config1"].ToString());
Debug.Print(settings.Rows[0]["Config2"].ToString());
// Etc..
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|