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 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.