Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML LINQ WinForm
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 1: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 at 15-May-13 23:09pm
   
Not sure why you are replying to this; the question is from last year.
Thirugnanam.R at 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 at 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 at 15-May-13 23:09pm
   
Not sure why you are replying to this; the question is from last year.
debkumar@codeproject at 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
0 OriginalGriff 7,903
1 Sergey Alexandrovich Kryukov 7,127
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,820


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 15 May 2013
Copyright © CodeProject, 1999-2014
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