LINQ to XML Cast Exception Error

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am attempting to read XML into a class (TripSheet) that contains a property which is another class(Patient). I dont know if I am taking the right approach with this. I need to say that I have it working fine IF I dont read the
XML into class (i.e. a variant list). Would someone look over my code and let me know if I am doing this the right way. The XML file is way too large to post here. When I run the program (console application) I get the following
error
System.InvalidCastException was unhandled<br/>
Message="Unable to cast object of type WhereSelectEnumerableIterator`2 [System.Xml.Linq.XElement,NEMSIS2_XML_Import.Module1+Patient] to type Patient[]."<br/>
Source="NEMSIS2_XML_Import"
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Imports System.Collections.Generic
<span style="color:Blue; Imports System.Text
<span style="color:Blue; Imports System.Xml
<span style="color:Blue; Imports System.Xml.Linq
<span style="color:Blue; Imports System.Linq
<span style="color:Blue; Imports System.Data.SqlClient
<span style="color:Blue; Imports System.Globalization
<span style="color:Blue; Module Module1
<span style="color:Blue; Sub Main()
<span style="color:Blue; Dim doc <span style="color:Blue; As XDocument = XDocument.Load(<span style="color:#A31515; "C:XMLDataemsdataset.xml")
<span style="color:Blue; Dim ns <span style="color:Blue; As XNamespace = doc.Root.Name.[<span style="color:Blue; Namespace]
<span style="color:Blue; Dim TripSheets <span style="color:Blue; As List(Of TripSheet) = (<span style="color:Blue; From record <span style="color:Blue; In doc.Descendants(ns + <span style="color:#A31515; "Record") _
<span style="color:Blue; Select <span style="color:Blue; New TripSheet() <span style="color:Blue; With _
{.PatientCareNumber = record.Descendants(ns + <span style="color:#A31515; "E01_01").First().Value, _
.PatientRecord = (<span style="color:Blue; From E6 <span style="color:Blue; In doc.Descendants(ns + <span style="color:#A31515; "E06") _
<span style="color:Blue; Select <span style="color:Blue; New Patient() <span style="color:Blue; With _
{ _
.LastName = E6.Descendants(ns + <span style="color:#A31515; "E06_01").Value, _
.FirstName = E6.Descendants(ns + <span style="color:#A31515; "E06_02").Value, _
.MiddleInitial = E6.Descendants(ns + <span style="color:#A31515; "E06_03").Value, _
.Address = E6.Descendants(ns + <span style="color:#A31515; "E06_04").Value, _
.City = E6.Descendants(ns + <span style="color:#A31515; "E06_05").Value, _
.State = E6.Descendants(ns + <span style="color:#A31515; "E06_07").Value, _
.Zip = E6.Descendants(ns + <span style="color:#A31515; "E06_08").Value, _
.Telephone = E6.Descendants(ns + <span style="color:#A31515; "E06_17").Value, _
.Gender = E6.Descendants(ns + <span style="color:#A31515; "E06_11").Value, _
.DateOfBirth = E6.Descendants(ns + <span style="color:#A31515; "E06_16").Value, _
.SocialSecurityNumber = E6.Descendants(ns + <span style="color:#A31515; "E06_10").Value _
}) _
}).ToList
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; #Region <span style="color:#A31515; " Trip Sheet Class "
<span style="color:Blue; Public <span style="color:Blue; Class TripSheet
<span style="color:Blue; Private _PatientCareNumber <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property PatientCareNumber() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _PatientCareNumber
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_PatientCareNumber = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Blue; Private _PatientRecord <span style="color:Blue; As Patient()
<span style="color:Blue; Public <span style="color:Blue; Property PatientRecord() <span style="color:Blue; As Patient()
<span style="color:Blue; Get
<span style="color:Blue; Return _PatientRecord
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As Patient())
_PatientRecord = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property

<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; #End Region


<span style="color:Blue; #Region <span style="color:#A31515; " Patient Class "
<span style="color:Blue; Public <span style="color:Blue; Class Patient
<span style="color:Blue; Private _LastName <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property LastName() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _LastName
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_LastName = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _FirstName <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property FirstName() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _FirstName
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_FirstName = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _MiddleInitial <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property MiddleInitial() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _MiddleInitial
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_MiddleInitial = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _Address <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property Address() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _Address
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_Address = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _City <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property City() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _City
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_City = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _State <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property State() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _State
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_State = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _Zip <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property Zip() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _Zip
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_Zip = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _Telephone <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property Telephone() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _Telephone
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_Telephone = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _Gender <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property Gender() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _Gender
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_Gender = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _DateOfBirth <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property DateOfBirth() <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Get
<span style="color:Blue; Return _DateOfBirth
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value <span style="color:Blue; As <span style="color:Blue; String)
_DateOfBirth = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; Private _SocialSecurityNumber <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Public <span style="color:Blue; Property SocialSecurityNumber()
<span style="color:Blue; Get
<span style="color:Blue; Return _SocialSecurityNumber
<span style="color:Blue; End <span style="color:Blue; Get
<span style="color:Blue; Set(<span style="color:Blue; ByVal value)
_SocialSecurityNumber = value
<span style="color:Blue; End <span style="color:Blue; Set
<span style="color:Blue; End <span style="color:Blue; Property
<span style="color:Blue; End <span style="color:Blue; Class
<span style="color:Blue; #End Region
<span style="color:Blue; End <span style="color:Blue; Module

[/code]


View the full article
 
Back
Top