tags:

views:

72

answers:

4

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?

A: 

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).

raf
A: 

You can use any of the following methods on XmlReader:

  • ReadSubtree()
  • ReadInnerXml()
  • ReadOuterXml()
  • ReadStartElement()

to read the child content. Which one you use depends on exactly what you wish to do with said child content.

Nick Jones
+1  A: 

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.

Robert Rossney
this was my thought too. If you can't use App.config, then serialization or just straight XPath...
Bryce Fischer
I'm looking into serialization right now. I'll need some time to figure out whether or not I can implement this.
Pieter
I tried to implement serialization in my program, but I'm getting a runtime error. Here are two screenshots: http://bit.ly/br5c03 http://bit.ly/8Zd2GC What am I doing wrong?
Pieter
When you get a type reflection error, look at the inner exception; it should tell you what's really going on. Also, see http://social.msdn.microsoft.com/Forums/en-US/xmlandnetfx/thread/e51b2d02-0fee-4cb4-9071-7ea3d5148c08, which won't tell you what's causing this error, probably, but will help you with what you're probably going to encounter next.
Robert Rossney
Thanks for your tip. I had a look at the inner exception - that helped solve a few issues. Those messages are really helpful when I'm debugging. I'm currently dealing with another issue, but that forum thread you referenced doesn't seem to deal with it. Screenshot: http://bit.ly/aygT4U `CalendarDB` is an `ArrayList` that contains `Calendar` objects. The InnerException is `{"Unable to cast object of type 'System.Object[]' to type 'ICS_Notifier.Calendar[]'."}`.
Pieter
The problem's that since `ArrayList` isn't strongly-typed, calling `ToArray()` on it creates an `object[]`, and you want a `Calendar[]`. Try casting the contents to `Calendar` first, e.g. `CalendarDB.Cast<Calendar>().ToArray()`.
Robert Rossney
Alright, the code is fully functional now. It sure is nice to be able to export data to XML with a few lines of code. Thank you!
Pieter
A: 

App.config and create a custom section.

App.Config and Custom Configuration Sections

Bryce Fischer