How to read a XML file?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: How to read a XML file?

  1. #1
    Join Date
    Jun 2009
    Posts
    6

    How to read a XML file?

    Hi everyone,

    I am having some problems to read a xml file. Here is part of it:
    <DataSources>
    <DataSource>
    <Company>P</Company>
    <UnitsFiles>
    <UnitFile>
    <Unit>Metric</Unit>
    <File>rec_setup_P.dat</File>
    </UnitFile>
    <UnitFile>
    <Unit>English</Unit>
    <File>rec_setup_PEnglish.dat</File>
    </UnitFile>
    <UnitFile>
    <Unit>Mexican</Unit>
    <File>rec_setup_PMexico.dat</File>
    </UnitFile>
    </UnitsFiles>
    </DataSource>
    <DataSource>
    <Company>I</Company>
    <UnitsFiles>
    <UnitFile>
    <Unit>Metric</Unit>
    <File>rec_setup_IMetric.dat</File>
    </UnitFile>
    <UnitFile>
    <Unit>English</Unit>
    <File>rec_setup_IEnglish.dat</File>
    </UnitFile>
    <UnitFile>
    <Unit>Mexican</Unit>
    <File>rec_setup_IMexico.dat</File>
    </UnitFile>
    </UnitsFiles>
    </DataSource>
    </DataSources>

    I know that the format is not the best but I can't change it.
    I need to get the companies with the units and files.
    I tried different things and the closest to the solution is:

    string FileName = "C:\\company.xml";
    XmlReader reader = XmlReader.Create(FileName);

    do{
    reader.ReadToFollowing("Company" );
    string Provider = reader.ReadInnerXml();
    List<string> Units = new List<string>();
    List<string> Files = new List<string>();
    reader.ReadToFollowing("UnitsFiles" );
    reader.ReadToFollowing("UnitFile" );
    do{
    //get units and wits file names
    reader.ReadToDescendant("Unit" );
    Units.Add(reader.ReadInnerXml());
    reader.ReadToFollowing("File" );
    Files.Add(reader.ReadInnerXml());
    } while (reader.ReadToNextSibling("UnitFile" ));
    ProviderUnitsWitsList.Add(new ProviderUnitsWitsFiles(Provider, Units, Files));
    } while (reader.ReadToFollowing("DataSource" ));

    The problem is that I get only one unit and one file per company.
    Does anyone know a solution to my problem?
    Thanks a lot.

  2. #2
    Join Date
    Jun 2008
    Posts
    2,477

    Re: How to read a XML file?

    Please use code tags, my eyes are hurting trying to read that.

    I would suggest using the XmlDocument class instead of an XmlReader.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center