[Mono-list] XML reading problem

Paul Johnson paul at all-the-johnsons.co.uk
Wed Mar 13 15:58:57 UTC 2013


Hi,

Got a small XML issue I could do with some help on.

I have an XML file that looks like this

<GirlRecord>
<FirstName>Girly</FirstName>
<LastName>Girl</LastName>
...
</GirlRecord>

The GirlRecord can contain upto 50 different fields - it doesn't have to 
contain all of them (so a record with 26 fields is fine).

I have a containing class which looks like this

public class GirlRecords
{
    public GirlRecords(){}

    public string FirstName
    {get;set;}
    public string LastName
    {get;set;}

    // and so on
}

To load the XML file back in, my code looks like this

private List<GirlRecords> ReadInGirlRecords()
         {
             List<GirlRecords> toReturn = new List<GirlRecords>();
             GirlRecords aff = null;
             int m = 0;
             string localPath = Path.Combine(path, "GirlRecords.xml");
             if (!File.Exists(localPath))
                 return toReturn;
             using (XmlReader reader = XmlReader.Create(localPath))
             {
                 aff = new GirlRecords();
                 while (reader.Read())
                 {
                     if (reader.IsStartElement())
                     {
                         switch (reader.Name)
                         {
                             case "FirstName":
                                 reader.Read();
                                 aff.FirstName = reader.Value;
                                 break;
                             case "LastName":
                                 reader.Read();
                                 aff.LastName = reader.Value;
                                 break;

The problem is that to add to the List, I need to find the terminating 
</GirlRecord> and act on that.

Question is, how do I do that? Should I use XmlReadInner or ReadOuter or 
is there a better way?

Paul

-- 
"Space," it says, "is big. Really big. You just won't believe how 
vastly, hugely, mindbogglingly big it is. I mean, you may think it's a 
long way down the road to the chemist's, but that's just peanuts to 
space, listen..."
Hitch Hikers Guide to the Galaxy, a truly remarkable book!



More information about the Mono-list mailing list