[英]Read CDATA section based on where clause using LINQ
I am trying to read CDATA section of an xml node based upon the where clause. 我试图基于where子句读取xml节点的CDATA部分。
<Book>
<BookItem ISBN="SKS84747"><![CDATA[20]]> </BookItem>
<BookItem ISBN="LHKGOI84747"><![CDATA[50]]> </BookItem>
<BookItem ISBN="PUOT84747"><![CDATA[20]]> </BookItem>
</Book>
This code gives me all the CDATA sections, 这段代码为我提供了所有CDATA部分,
var value = from v in x.Descendants("BookItem").OfType<XCData>()
select (string)v.Value;
How to put where clause based on ISBN ? 如何根据ISBN放置where子句? How can I read this CDATA using LINQ to XML. 如何使用LINQ to XML读取此CDATA。
var value = x.DescendantNodes().OfType<XCData>()
.Where(m => m.Parent.Name == "BookItem" && m.Parent.Attribute("ISBN").Value == "PUOT84747")
.ToList();
or 要么
before ToList() 在ToList()之前
.Select(cdata => cdata.Value.ToString());
var xml = Resource1.String1;
var doc = XDocument.Parse(xml);
var isbn = "SKS84747";
var query = string.Format("BookItem[@ISBN='{0}']", isbn);
var book = doc.Root.XPathSelectElement(query);
if (book != null)
Console.WriteLine(book.Value);
OR 要么
var book =
doc.Root.Descendants("BookItem").Where(
x => x.Attribute("ISBN") != null && x.Attribute("ISBN").Value == isbn).Select(x => x.Value).
FirstOrDefault();
OR 要么
var book = (from item in doc.Root.Descendants("BookItem")
where item.Attributes("ISBN").Any() && item.Attribute("ISBN").Value == isbn
select item.Value).FirstOrDefault();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.