Click here to Skip to main content
15,886,778 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi friends,

I have XML with CDATA.
When i get the data from Linq to XML then it will give me error ''.', hexadecimal value 0xFFFF, is an invalid character'.
My code is as below:

C#
string serializedObject = PhysicalResult.XmlSerialize();
                XElement xmlData = XElement.Parse(serializedObject);
                var result = (from child in xmlData.Elements("SMS")
                                         select child).ToList();



So please help me how to read CDATA using Linq to XML..

Thanks,
Viprat
Posted

C#
XDocument message = new XDocument(XDocument.Parse(xmlString));

1.  XCData cdata = message.DescendantNodes().OfType<XCData>().Where(m => m.Parent.Name == "iDa").ToList()[0];
string cDataContent = cdata.Value;
2.  string cDataContent =message.Root.Descendants("iDa").First().Value;

 Where iDa => Holds the CDATA value


For More Info Blog
 
Share this answer
 
v2
Comments
H.Brydon 15-May-13 23:09pm    
Not sure why you are replying to this; the question is from last year.
Thirugnanam.R 16-May-13 1:36am    
Sorry I haven't noted the date. Will check in future
C#
 XElement XTemp = XElement.Load(YourXMLfile);  
var queryCDATAXML = from element in XTemp.DescendantNodes()
                         where element.NodeType == System.Xml.XmlNodeType.CDATA
                         select element.Parent.Value.Trim();
 
Share this answer
 
Comments
VIPR@T 18-Sep-12 6:47am    
Thanks for the reply. But using this query it will read only those records whose nodetype is CDATA. I need all the records. With CDATA as well as With out CDATA.
You may create code snippet similar to below one.

C#
var cdataElement = xmlobj.DescendantNodes().OfType<XCData>().FirstOrDefault();
if (cdataElement != null)
    cdataContent= cdataElement.Value;
 
Share this answer
 
v2
Comments
H.Brydon 15-May-13 23:09pm    
Not sure why you are replying to this; the question is from last year.
debkumar@codeproject 18-May-13 9:55am    
I am sorry. That was my mistake. I did not notice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900