Hi Everyone,

I am trying to read in a block of xml using the following code, but it always jumps over the foreach loop:

                XDocument doc = XDocument.Load(sourceFile);
                XElement root = XElement.Load(sourceFile);

                var DataRecords = doc.Elements("DATA_RECORD");
                foreach (var dr in DataRecords)
                {
                    //build up the values we're interested in
                    string refno = dr.Attribute("NGIE_REFNO").Value;
                    string title = dr.Attribute("NGIE_TITLE").Value;
                    string initials = dr.Attribute("NGIE_INITIALS").Value;
                    string surname = dr.Attribute("NGIE_SURNAME").Value;
                    string forename = dr.Attribute("NGIE_FORENAME").Value;
                    string dob = dr.Attribute("LAG_DOB").Value;
                    string fullName = dr.Attribute("NGIE_FULL_NAME").Value;
                    string other_name = dr.Attribute("LAG_PAR_OTHER_NAME").Value;
                    string gender = dr.Attribute("LAG_PAR_GENDER").Value;
                    string disabled = dr.Attribute("NGIE_DISABLED_IND").Value;
                    string risk_ind = dr.Attribute("NGIE_RISK_IND").Value;
                    string telephone = dr.Attribute("NGIE_TELEPHONE").Value;
                    string mobile = dr.Attribute("NGIE_MOBILE").Value;
                    string work = dr.Attribute("NGIE_WORK").Value;
                    string fax = dr.Attribute("NGIE_FAX").Value;

                    //and then populate them into the new record
                    FWTIndividualUpdate fwtIndividualUpdate = new FWTIndividualUpdate();

                    if (!string.IsNullOrEmpty(dob))
                    {
                        fwtIndividualUpdate.DateOfBirthUpdate.DateOfBirth = DateTime.Parse(dob);
                    }

                    int result = inUpdate.updateIndividual(fwtIndividualUpdate);
                }

With the xml looking like:

<main>
 <DATA_RECORD>
  <NGIE_REFNO>318</NGIE_REFNO> 
  <NGIE_TITLE>MISS</NGIE_TITLE> 
  <NGIE_INITIALS>A</NGIE_INITIALS> 
  <NGIE_SURNAME>Flogget</NGIE_SURNAME> 
  <NGIE_FORENAME>H</NGIE_FORENAME> 
  <LAG_DOB>1969-01-31</LAG_DOB> 
  <NGIE_FULL_NAME>MISS A Flogget</NGIE_FULL_NAME> 
  <LAG_PAR_OTHER_NAME /> 
  <LAG_PAR_GENDER>F</LAG_PAR_GENDER> 
  <NGIE_DISABLED_IND>N</NGIE_DISABLED_IND> 
  <NGIE_RISK_IND>1</NGIE_RISK_IND> 
  <LAG_PAR_POST_BOX_NUMBER /> 
  <NGIE_TELEPHONE>TELEPHONE</NGIE_TELEPHONE> 
  <NGIE_MOBILE>MOBILE</NGIE_MOBILE> 
  <NGIE_WORK>WORK</NGIE_WORK> 
  <NGIE_FAX>FAX</NGIE_FAX> 
 </DATA_RECORD>
</main>

Any ideas would be greatly appreciated.

This seems to mean that DataRecords is somehow empty and so doc.Elements is probably empty also. Did you check that?

Hi ddande..

No the doc.Elements is populated...

<!DOCTYPE main (View Source for full doctype...)> 
  <main>
  <DATA_RECORD>
  <NGIE_REFNO>318</NGIE_REFNO> 
  <NGIE_TITLE>MISS</NGIE_TITLE> 
  <NGIE_INITIALS>H</NGIE_INITIALS> 
  <NGIE_SURNAME>******</NGIE_SURNAME> 
  <NGIE_FORENAME>H</NGIE_FORENAME> 
  <LAG_DOB>1967-01-31</LAG_DOB> 
  <NGIE_FULL_NAME>MISS H ******</NGIE_FULL_NAME> 
  <LAG_PAR_OTHER_NAME /> 
  <LAG_PAR_GENDER>F</LAG_PAR_GENDER> 
  <NGIE_DISABLED_IND>N</NGIE_DISABLED_IND> 
  <NGIE_RISK_IND>1</NGIE_RISK_IND> 
  <LAG_PAR_POST_BOX_NUMBER /> 
  <NGIE_TELEPHONE>TELEPHONE</NGIE_TELEPHONE> 
  <NGIE_MOBILE>MOBILE</NGIE_MOBILE> 
  <NGIE_WORK>WORK</NGIE_WORK> 
  <NGIE_FAX>FAX</NGIE_FAX> 
  </DATA_RECORD>
  <DATA_RECORD>
  <NGIE_REFNO>319</NGIE_REFNO> 
  <NGIE_TITLE>MISS</NGIE_TITLE> 
  <NGIE_INITIALS>K</NGIE_INITIALS> 
  <NGIE_SURNAME>******</NGIE_SURNAME> 
  <NGIE_FORENAME>K</NGIE_FORENAME> 
  <LAG_DOB>1987-05-25</LAG_DOB> 
  <NGIE_FULL_NAME>MISS K ******</NGIE_FULL_NAME> 
  <LAG_PAR_OTHER_NAME /> 
  <LAG_PAR_GENDER>F</LAG_PAR_GENDER> 
  <NGIE_DISABLED_IND>N</NGIE_DISABLED_IND> 
  <NGIE_RISK_IND>1</NGIE_RISK_IND> 
  <NGIE_ADDRESS_2>LEMINGTON</NGIE_ADDRESS_2> 
  <NGIE_ADDRESS_3>NEWCASTLE UPON TYNE</NGIE_ADDRESS_3> 
  <NGIE_POSTCODE>*** ***</NGIE_POSTCODE> 
  <LAG_PAR_POST_BOX_NUMBER /> 
  <NGIE_TELEPHONE>TELEPHONE</NGIE_TELEPHONE> 
  <NGIE_MOBILE>MOBILE</NGIE_MOBILE> 
  <NGIE_WORK>WORK</NGIE_WORK> 
  <NGIE_FAX>FAX</NGIE_FAX> 
  </DATA_RECORD>
  </main>

b but the DataRecords is empty....

DataRecords only gets the name populated with "DATA_RECORD" after it jumps over the foreach loop.

Elements() only retrieves direct child nodes. XDocument will only have child node, the root node (main in this case).

You would have to use the XDocument.Root property to access the root node (you should not be loading that from the file as well; get rid of line 2 of your first code snippet). Then use Elements() on it, like so:

doc.Root.Elements("DATA_RECORD");

FYI, there is also a Descendants() method that would work on the document, as it searches all child nodes (ie. child node, children of child node, etc.).

Hi nmailnet,

Thanks very much, that did the job. Got into the loop and the first attribut 'refno' I got hit with a NullReferenceException was unhandled and states Object reference not set to an instance of an object..

You're welcome.

The child nodes of DATA_RECORD are not attributes, they are elements. Attributes look like this:

<DATA_RECORD NGIE_REFNO="318">
    ...
</DATA_RECORD>

whereas elements look like this:

<DATA_RECORD>
    <NGIE_REFNO>318</NGIE_REFNO>
    ...
</DATA_RECORD>

EDIT: Here's a pretty good tutorial on XML: http://w3schools.com/xml/default.asp

Hi nmailet,

That link was just what I needed, thanks. Good diagram of the bookstore on there that explained it.

Glad I could help. Please mark the thread solved unless you had any more questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.