4

I have the following xml file and I am trying to use linq to xml to get the Elements which are residing inside the CDATA section. Any suggestions please.

<?xml version = "1.0" encoding = "UTF-8"?>
<result final = "true" transaction-id="84WO" xmlns="http://cp.com/rules/client">
<client id = "CustName'>
<quoteback>
</client>
<report format = "CP XML">
<![CDATA[<?xml version="1.0" encoding = "UTF-8" standalone = "yes"?>
 <personal_auto xmlns = "http://cp.com/rules/client">
    <admin>
    </admin>
    <report>
    </report>
 </personal_auto>
]]>
</report> </result>
3
  • Please edit your question (and this time use a preview) as I don't see anything in your XML sample. Commented Sep 15, 2010 at 17:21
  • Your XML is invalid. The <?xml line must be the first line. Commented Sep 15, 2010 at 17:54
  • Still missing closingg restult tag. Commented Sep 15, 2010 at 17:58

3 Answers 3

4
 XElement XTemp = XElement.Load(YourXMLfile);  
var queryCDATAXML = from element in XTemp.DescendantNodes()
                         where element.NodeType == System.Xml.XmlNodeType.CDATA
                         select element.Parent.Value.Trim(); 
Sign up to request clarification or add additional context in comments.

Comments

1

This is standard LINQ functionality - see http://msdn.microsoft.com/en-us/library/system.xml.linq.xcdata.aspx

Could you please explain the problem in more detail if this doesn't solve it?

7 Comments

My requirement is to get all the elements inside the CDATA section how can i get it using LINQ to XML
Ah, you want someone to do your job for you. Not actually answer a question. Right. One quick Google later: jarvis.com.au/post/2008/09/I-love-Linq-to-XML.aspx
Rushyo. sorry. My XML file has an XML file inside CDATA section. The task that I am trying to achieve here is that I need to be able to navigate the xml file inside CDATA section and extract the necessary elements. any suggestions please?
Right. So what you're saying is you have an XML document inside an XML document? All within a LINQ statement? Not sure that's possible, though I'd be happy for someone to correct me. Generally speaking, I can't imagine someone doing that.
I'd suggest just grabbing the CDATA element you want, loading the document, then executing a second query on it.
|
0

I was looking to do something slightly different - I'm embedding sql in xml using cdata in its own dedicated element named 'sql'

Just to clarify cdata content will be read transparently.

if you do

var cdataContent = sql.Value;

you get whatever string is in the

 <![CDATA[..]]>

tag without having to instantiate a different node type on top of it or do anything fancy

so in the above case cdataContent would just be "..".

Linq to sql really is nice! I always expect there to be more messing about involved. Hats off to the guys who made that API.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.