Click here to Skip to main content
12,253,114 members (53,901 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML LINQ
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:

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 18-Sep-12 0:22am
VIPR@T2.8K
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

 XElement XTemp = XElement.Load(YourXMLfile);  
var queryCDATAXML = from element in XTemp.DescendantNodes()
                         where element.NodeType == System.Xml.XmlNodeType.CDATA
                         select element.Parent.Value.Trim();
  Permalink  
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

You may create code snippet similar to below one.

var cdataElement = xmlobj.DescendantNodes().OfType<XCData>().FirstOrDefault();
if (cdataElement != null)
    cdataContent= cdataElement.Value;
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160426.1 | Last Updated 15 May 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100