how do I read the following xml document?
My code is :
var vb =
(from vbs in XMLDoc.Descendants("Response").Descendants("Records ")
select new
{
ref = vbs.Element("ref").Value
}).ToList();
XML Document:
<Response>
<Msg>
<Code>30</Code>
<Query/>
</Msg>
<Rec>
<Records price="1989" no="838976" ref="1927A64FF6B03527E5BFD8424F647848005143DB" query="00"/>
</Rec>
</Response>
"Records "should be"Records", and your call toElement()in the anonymous class member initializer should beAttribute()since you are reading an attribute, which is not an element.The conversion to
stringis preferred when reading attributes, IMO, because it will returnnullwhen the attribute cannot be found. If you usevbs.Attribute("ref").Valueinstead, then you will cause aNullReferenceExceptionif the attribute does not exist.