I have xml:
<?xml version="1.0" encoding="utf-8" ?>
<books>
<book>
<author>Ray</author>
<album>Other</album>
<cover size="large">LargeCover</cover>
<cover size="mini">MiniCover</cover>
</book>
</books>
How to get string "MiniCover"?
I wrote the code, but it does not work — string is empty;
string testLink = (@"Text.xml");
XDocument xml = XDocument.Load(testLink);
string cv = String.Empty;
var c = from cover in xml.Elements("book")
where (string)cover.Attribute("size").Value == "mini"
select cover.Value;
foreach (var item in c)
{
cv += item.ToString();
}
MessageBox.Show(cv);
Thanks!
When using
Elements()
you have to specify the structure more precisely.In your code,
cover
is a<book>
element. Butsize
is an attribute of<cover>
.This ought to work:
Xpath can simplify your code
to get the inner text