-2

I have question about searching/filtering in my XML file via c#

I have a big XML files containing different schools in my country:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<data-set xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <school>
        <schoolnummer>3699</schoolnummer>
        <vestigingsnummer>1</vestigingsnummer>
        <net>Vrij gesubsidieerd onderwijs</net>
        <naam>Vrije Basisschool -           Sint-Joris</naam>
        <hoofdzetel>J</hoofdzetel>
        <straat>Cellebroersstraat</straat>
        <huisnummer>16</huisnummer>
        <postcode>1000</postcode>
        <gemeente>BRUSSEL</gemeente>
        <crabcode>19636</crabcode>
        <crabhuisnr>16</crabhuisnr>
        <crabbusnr>     </crabbusnr>
        <telefoon>0471-76.32.05</telefoon>
        <email>pdedonder@sintjorisbasisschool.be</email>
        <url>www.sintjorisbasisschool.be</url>
        <familienaambeheerder>DE DONDER</familienaambeheerder>
        <voornaambeheerder>Peter</voornaambeheerder>
    </school>
    <school>
        <schoolnummer>3889</schoolnummer>
        <vestigingsnummer>1</vestigingsnummer>
        <net>Vrij gesubsidieerd onderwijs</net>
        <naam>Vrije Basisschool</naam>
        <hoofdzetel>J</hoofdzetel>
        <straat>John Waterloo Wilsonstraat</straat>
        <huisnummer>21</huisnummer>
        <postcode>1000</postcode>
        <gemeente>BRUSSEL</gemeente>
        <crabcode>19972</crabcode>
        <crabhuisnr>21</crabhuisnr>
        <crabbusnr>     </crabbusnr>
        <telefoon>02-230.75.28</telefoon>
        <fax>02-230.48.44</fax>
        <email>abeullens@tennude.be</email>
        <url>www.tennude.be</url>
        <familienaambeheerder>BEULLENS</familienaambeheerder>
        <voornaambeheerder>Ann</voornaambeheerder>
    </school>

For my project I need to find all the <School>s by searching for the right <postcode>, for example '1000'.

What is the best way to do this? XMLReader / LINQ to XML? Note: It's a very big XML file.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Niels
  • 416
  • 5
  • 22

1 Answers1

1

I'm a big fan of LINQ to XML, but for strongly structured, table-like XML data like yours I would always recommend XmlSerializer. It a much more specialized, high-level representation of your data than XDocument and XElement.

You can define a C# class named School, mark it with [XmlRoot("school")], define C# properties for each XML field (i.e. public int schoolnummer { get; set; } etc.) and fill your School objects using XmlSerializer.Deserialize(). It automatically finds your C# properties by the element names in the XML (case-sensitive!).

And if your XML file is too large to keep everything in RAM at the same time, you have to find a way to deserialize objects and throw them away while you are still searching.

I have a sample here that may help you. I programmed it a few months ago, and I combined XmlTextReader with XmlSerializer to read TestUser objects from users.xml one by one and add them to a list. Not exactly what you want to do, but you can adapt it to suit your needs:

private void _LoadUsers()
{
    _users = new List<TestUser>();

    string path = Path.Combine(_projectNamespace, "users.xml");
    var stream = new FileStream(path, FileMode.Open);
    var reader = new XmlTextReader(new StreamReader(stream));
    while (reader.ReadToFollowing("user"))
    {
        var serializer = new XmlSerializer(typeof(TestUser));
        _users.Add((TestUser)serializer.Deserialize(reader.ReadSubtree()));
    }
    stream.Close();
}
Kim Homann
  • 3,042
  • 1
  • 17
  • 20