SYSTEM.XML in VB.NET - how to ignore missing elements?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am using System.xml in VB.NET to parse data from a survey result XML file.
I am getting an error when encountering the HCAHPS element for the second patient.
How can I set his up to just ignore non-existing elements? Basically return empty strings?

Dim PatientSection = From PatientItem In xDoc.Descendants("PATIENTLEVELDATA") _<br/>
Select New With { _<br/>
.SurveyID = PatientItem.Element("SURVEY_ID").Value, _<br/>
.ClientID = PatientItem.Element("CLIENT_ID").Value, _<br/>
.Service = PatientItem.Element("SERVICE").Value, _<br/>
.RecDate = PatientItem.Element("RECDATE").Value, _<br/>
.DisDate = PatientItem.Element("DISDATE").Value, _<br/>
.HCAHPSVarname = PatientItem.Element("HCAHPS").Element("RESPONSE").Element("VARNAME").Value, _<br/>
.HCAHPSValue = PatientItem.Element("HCAHPS").Element("RESPONSE").Element("VALUE").Value}
<?xml version="1.0" encoding="UTF-8"?><br/>
<PATIENTLEVELDATA><br/>
<SURVEY_ID>475732415</SURVEY_ID><br/>
<CLIENT_ID>2561</CLIENT_ID><br/>
<SERVICE>PY</SERVICE><br/>
<RECDATE>2012-01-23</RECDATE><br/>
<DISDATE></DISDATE><br/>
<ANALYSIS><br/>
<RESPONSE><br/>
<VARNAME>TT1</VARNAME><br/>
<VALUE>5</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>TT2</VARNAME><br/>
<VALUE>5</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>TT3</VARNAME><br/>
<VALUE>5</VALUE><br/>
</RESPONSE><br/>
</ANALYSIS><br/>
<DEMOGRAPHICS><br/>
<RESPONSE><br/>
<VARNAME>SEX</VARNAME><br/>
<VALUE>Male</VALUE><br/>
</RESPONSE><br/>
</DEMOGRAPHICS><br/>
<HCAHPS><br/>
<RESPONSE><br/>
<VARNAME>CMS_6</VARNAME><br/>
<VALUE>Always</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>CMS_7</VARNAME><br/>
<VALUE>Always</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>CMS_8</VARNAME><br/>
<VALUE>Always</VALUE><br/>
</RESPONSE><br/>
</HCAHPS><br/>
</PATIENTLEVELDATA><br/>
<PATIENTLEVELDATA><br/>
<SURVEY_ID>475732418</SURVEY_ID><br/>
<CLIENT_ID>2561</CLIENT_ID><br/>
<SERVICE>PY</SERVICE><br/>
<RECDATE>2012-01-25</RECDATE><br/>
<DISDATE>2012-01-17</DISDATE><br/>
<ANALYSIS><br/>
<RESPONSE><br/>
<VARNAME>A1</VARNAME><br/>
<VALUE>4</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>A2</VARNAME><br/>
<VALUE>4</VALUE><br/>
</RESPONSE><br/>
<RESPONSE><br/>
<VARNAME>A3</VARNAME><br/>
<VALUE>4</VALUE><br/>
</RESPONSE><br/>
</ANALYSIS><br/>
<DEMOGRAPHICS><br/>
<RESPONSE><br/>
<VARNAME>SEX</VARNAME><br/>
<VALUE>Male</VALUE><br/>
</RESPONSE><br/>
</DEMOGRAPHICS><br/>
<HCAHPS><br/>
</HCAHPS><br/>
</PATIENTLEVELDATA>

View the full article
 
Back
Top