[Mono-list] XML reading problem

Ian Norton ian.norton-badrul at thales-esecurity.com
Wed Mar 13 19:13:40 UTC 2013


Have you tried XmlDocument? 

On Wed, Mar 13, 2013 at 03:58:57PM +0000, Paul Johnson wrote:
> 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!
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list


More information about the Mono-list mailing list