Does anyone know how to cast XmlCDataSection to XDocument in C#?
This is what I have so far but its not working:
XmlCDataSection xcData = xcDataInput as XmlCDataSection;
XDocument xdoc = xcData.Cast<XmlNode>().Select(node => XDocument.Parse(node.OuterXml).Root);
This is the error:
Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'System.Xml.Linq.XDocument'. An explicit conversion exists (are you missing a cast?)
It's not clear why you're using Cast
or Select
at all. I suspect you just want:
XDocument xdoc = XDocument.Parse(xcData.InnerText);
Note that I'm using InnerText
rather than OuterXml
, as the outer XML of a CDATA node will never be a valid XML document on its own. If this isn't what you're looking for, please show a complete example of a document containing a CDATA section, and the document you want to get out of it.
See more on this question at Stackoverflow