Reputation:
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>
Upvotes: 0
Views: 164
Reputation: 169498
"Records "
should be "Records"
, and your call to Element()
in the anonymous class member initializer should be Attribute()
since you are reading an attribute, which is not an element.
var vb =
(from vbs in XMLDoc.Descendants("Response").Descendants("Records")
select new
{
ref = (string)vbs.Attribute("ref")
}).ToList();
The conversion to string
is preferred when reading attributes, IMO, because it will return null
when the attribute cannot be found. If you use vbs.Attribute("ref").Value
instead, then you will cause a NullReferenceException
if the attribute does not exist.
Upvotes: 3