[Mono-list] XML reading problem
Daniel Lo Nigro
lists at dan.cx
Thu Mar 14 02:33:04 UTC 2013
This sounds like a job for the XML serializer (eg.
http://geekswithblogs.net/TimH/archive/2006/02/09/68857.aspx). You should
be able to use an XML deserializer (either the standard .NET one or a
custom one) to parse your XML into objects. Mapping XML to objects is
pretty common and a XML deserializer should be able to do most of the work
for you. :)
Otherwise, I'd suggest using LINQ to XML instead. Use XElement.Load to load
the XML file, file.Descendants("GirlRecord") to get the records and
record.Element("FirstName") to get the value.
On Thu, Mar 14, 2013 at 2:58 AM, Paul Johnson
<paul at all-the-johnsons.co.uk>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<http://lists.ximian.com/mailman/listinfo/mono-list>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130314/29eca226/attachment-0001.html>
More information about the Mono-list
mailing list