Extract data from CDATA using LINQ to XML

I have the following xml file and I'm trying to use linq for xml to get the elements that are 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> 
+4
source share
3 answers
  XElement XTemp = XElement.Load(YourXMLfile); var queryCDATAXML = from element in XTemp.DescendantNodes() where element.NodeType == System.Xml.XmlNodeType.CDATA select element.Parent.Value.Trim(); 
+4
source

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

Could you explain the problem in more detail if this does not solve it?

+1
source

I was looking for something a little different - I embed sql in xml using cdata in my own selected element named 'sql'

Just to clarify the contents of cdata will be read transparently.

if you do

 var cdataContent = sql.Value; 

you get any line in

  <![CDATA[..]]> 

without having to instantiate another type of node on top of it or do something fantastic

therefore, in the above case, cdataContent will just be "..".

Linq to sql is really nice! I always expect that there will be more messing around. Hats for the guys who made this API.

0
source

All Articles