Steve Sirica
asked on
Using Linq to read an XML file with generic list
I have the following XML file structure:
I have the following classes:
I thought I could get a record using the following function:
I get the following error:
"Unable to cast object of type 'WhereSelectEnumerableIter ator`2[Sys tem.Xml.Li nq.XElemen t,clsFeat] ' to type 'System.Collections.Generi c.List`1[c lsFeat]'."
So what am I doing wrong?
<?xml version="1.0" encoding="utf-8" ?>
<Rates>
<CarrierRatePlan Name="BMNBIBPNTN450UNWUMM">
<OBRatePlan>WLPL1</OBRatePlan>
<RateNetCode>BAMBL</RateNetCode>
<Feats>
<Feat>
<Code>WIRELE</Code>
<NetCode>NONET</NetCode>
</Feat>
<Feat>
<Code>WLVMS1</Code>
<NetCode>NONET</NetCode>
</Feat>
</Feats>
</CarrierRatePlan>
</Rates>
I have the following classes:
Public Class clsRate
Public Property CarrierRatePlan As String
Public Property OBRatePlan As String
Public Property RateNetCode As String
Public Property Feats As List(Of clsFeat)
End Class
Public Class clsFeat
Public Property Code As String
Public Property NetCode As String
End Class
I thought I could get a record using the following function:
Public Function GetRate(aRate As String) As clsRate
Dim xml As XDocument
Try
xml = XDocument.Load(Server.MapPath("~/RateRefFile.xml"))
Return (From c In xml.Descendants("CarrierRatePlan")
Where c.Attribute("Name").Value.Equals(aRate.Trim)
Select New clsRate() With
{
.CarrierRatePlan = c.Attribute("Name").Value,
.OBRatePlan = c.Element("OBRatePlan").Value,
.RateNetCode = c.Element("RateNetCode").Value,
.Feats = (From f In c.Descendants("Feats")
Select New clsFeat() With
{
.Code = f.Element("Code").Value,
.NetCode = f.Element("NetCode").Value
})
}
).FirstOrDefault()
Catch ex As Exception
Throw
End Try
End Function
I get the following error:
"Unable to cast object of type 'WhereSelectEnumerableIter
So what am I doing wrong?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Not a Problem SSirica, glad I was able to help.
ASKER