English 中文(简体)
How can we query XCData under a particular XElment Node?
原标题:
<XMLDOC> 
<OPTIONA>   
    <![CDATA[
aaaaaaaaaaaaa           
]]>
    <![CDATA[
    bbbbbbbb]]>
<OPTIONA>
<OPTIONB>
    <![CDATA[
cccccccccccccccccccc            
]]>
    <![CDATA[
   dddddddddddddd]]>
</OPTIONB>
</XMLDOC>

How do I query say all CDATA s under OPTIONB?? using Linq-to-XML???

最佳回答

The OPTIONB node is equivalent to:

<OPTIONB>
    <![CDATA[
cccccccccccccccccccc                    

   dddddddddddddd]]>
</OPTIONB>

So to get the value inside the CData section you could use the following:

var cdata = XElement.Load("test.xml").Element("OPTIONB").Value;

You will not be able to get the CData values separately because they have the same semantics as if it was a single CData section for a XML parser.

问题回答
XElement.Load("test.xml")
 .Element("OPTIONB")
   .Nodes()
     .Where(x=>x is XCData).First().Cast<XCData>().Value




相关问题
Updating Linq to XML element values concatenation issues

I am trying to write a app.config / web.config error correcting app which will audit our developers applications for incorrect environment settings. I am using Linq to XML to accomplish this and I am ...

Is LINQ to XML s XElement ordered?

When I use LINQ to XML, is the order of the elements and attributes written out to text guaranteed to be the same order as how I added the XElement and XAttribute objects? Similarly, when I read in ...

热门标签