Reputation: 32805
I'm looking into the possibility of storing settings in an XML file. Here's a simplified version of my code. You don't see it here, but this code is located inside a try
block so that I can catch any XmlException
that comes up.
XmlReader fileReader = XmlReader.Create(Path.GetDirectoryName(Application.ExecutablePath) + "\\settings.xml");
// Start reading
while (fileReader.Read())
{
// Only concern yourself with start tags
if (fileReader.IsStartElement())
{
switch (fileReader.Name)
{
// Calendar start tag detected
case "Calendar":
//
//
// Here's my question: can I access the children
// of this element here?
//
//
break;
}
}
}
// Close the XML reader
fileReader.Close();
Can I access the children of a certain element on the spot in the code where I put the big comment?
Upvotes: 1
Views: 798
Reputation: 5432
App.config and create a custom section.
App.Config and Custom Configuration Sections
Upvotes: 0
Reputation: 96860
There are applications where using XmlReader
to read through an XML document is the right answer. Unless you have hundreds of thousands of user settings that you want to read (you don't) and you don't need to update them (you do), XmlReader
is the wrong choice.
Since you're talking about wanting to store arrays and structs in your XML, it seems obvious to me that you want to be using .NET's built in serialization mechanisms. (You can even use XML as the serialization format, if accessing the XML is important to you.) See this page for a starting point.
If you design your settings classes properly (which is not hard), you can serialize and deserialize them without having to write code that knows anything about the names and data types of the properties you're serializing. Your code that accesses the settings will be completely decoupled from the implementation details of how they are persisted, which, as Martha Stewart would say, is a Good Thing.
Upvotes: 1
Reputation: 6493
You can use any of the following methods on XmlReader:
to read the child content. Which one you use depends on exactly what you wish to do with said child content.
Upvotes: 0
Reputation: 156
Check this short article for a solution.
And by the way, you should use LINQ to XML if you want to work easy with XML (overview).
Upvotes: 0