Using Linq to read an XML file with generic list

I have the following XML file structure:
<?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>

Open in new window


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

Open in new window


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

Open in new window


I get the following error:

"Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,clsFeat]' to type 'System.Collections.Generic.List`1[clsFeat]'."

So what am I doing wrong?
LVL 4
Steve SiricaSenior Web Application EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
Hi SSirica;

I made two changes to this section of the query, One I change Feats to Feat so that it accessed the correct level of the document and the other was to convert the IEnumerable to a List by adding .ToList() at the end of the subquery.
.Feats = (From f In c.Descendants("Feat")
          Select New clsFeat() With
                  {
                  .Code = f.Element("Code").Value,
                  .NetCode = f.Element("NetCode").Value
                  }).ToList()
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Steve SiricaSenior Web Application EngineerAuthor Commented:
Outstanding!  Thank you Fernando.  I knew I was close.  Thanks for getting me the rest of the way.
0
Fernando SotoRetiredCommented:
Not a Problem SSirica, glad I was able to help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.