View Single Post
  #1 (permalink)  
Old December 17th, 2007, 06:22 PM
Sojan80 Sojan80 is offline
Authorized User
 
Join Date: Jan 2006
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Help Parsing XML File

I'm using a C# Console Application. I have an XML and matching XSD File. Here's a small sample of my XML:
<DataFile>
  <Header>
    <FileDate>5/23/2007 3:03:05 PM</FileDate>
    <FileVersion>SomeFileVersion</FileVersion>
    <AppType>NotDefined</AppType>
    <AcadLevel>Undergraduate</AcadLevel>
    <CampusCode>Madison</CampusCode>
    <ApplicationCount>18</ApplicationCount>
    <BatchID>cc71fd48-ff52-47ae-a753-3b92f5ebbd29</BatchID>
  </Header>
  <SubmittedApplications>
    <Application>
        <BIRTH_DATE>19720505</BIRTH_DATE>
        <FIRST_NAME>Mary</FIRST_NAME>
        <HSCOURSE id="1">
            <HSCOURSE_CREDITS>3</HSCOURSE_CREDITS>
            <HSCOURSE_DEPARTMENT>English</HSCOURSE_DEPARTMENT>
            <HSCOURSE_TERM>Fall Sem</HSCOURSE_TERM>
            <HSCOURSE_TERM_OTHER />
            <HSCOURSE_TITLE>British Literature</HSCOURSE_TITLE>
            <HSCOURSE_YEAR>20072008</HSCOURSE_YEAR>
        </HSCOURSE>
        <HSCOURSE id="2">
            <HSCOURSE_CREDITS>5</HSCOURSE_CREDITS>
            <HSCOURSE_DEPARTMENT>Chemistry</HSCOURSE_DEPARTMENT>
            <HSCOURSE_TERM>Acad Year</HSCOURSE_TERM>
            <HSCOURSE_TERM_OTHER />
            <HSCOURSE_TITLE>AP Chemistry</HSCOURSE_TITLE>
            <HSCOURSE_YEAR>20062007</HSCOURSE_YEAR>
        </HSCOURSE>
        <LAST_NAME>Mittelstadt</LAST_NAME>
    </Application>
  </SubmittedApplications>
</DataFile>

What I need to do is:
- pass in an array of node names e.g. string[] nodeList = { "FIRST_NAME", "LAST_NAME", "SSN", "BIRTH_DATE", "HSCOURSE" };
- loop through each application node in the XML file obtaining all instances for that application of the nodes (and their children if any) passed in the array including attributes for each of the nodes passed in
- write those values to screen then go to the next application in the file and so on.

I can load the xml into an XmlDocument and am able to create a nodelist containing all of the application nodes.

I cannot however get the sequence to allow me to loop through through the application nodes retrieving all the instances of the nodes passed in. For example there could be no <HS_COURSE> listings in which case it shows up as <HS_COURSE/> or there could be ten of them and I need to be able to grab them all.

How do I go about looping through each application obtaining all instances of the given nodes in the array as well as any child nodes that node may contain?

Reply With Quote