As for this issue, I suggest you could check the object class. When you set the property, you need to check whether it is null. You could refer to the following code:
[XmlRoot("root")]
public class Sample1Xml
{
[XmlElement("node")]
public NodeType Node { get; set; }
#region Nested type: NodeType
public class NodeType
{
[XmlAttribute("attr1")]
public string Attr1 { get; set; }
[XmlAttribute("attr2")]
public string Attr2 { get; set; }
[XmlIgnore]
public string Content { get; set; }
[XmlText]
public XmlNode[] CDataContent
{
get
{
var dummy = new XmlDocument();
return new XmlNode[] {dummy.CreateCDataSection(Content)};
}
set
{
if (value == null)
{
Content = null;
return;
}
if (value.Length != 1)
{
throw new InvalidOperationException(
String.Format(
"Invalid array length {0}", value.Length));
}
var node0 = value[0];
var cdata = node0 as XmlCDataSection;
if (cdata == null)
{
throw new InvalidOperationException(
String.Format(
"Invalid node type {0}", node0.NodeType));
}
Content = cdata.Data;
}
}
}
#endregion
}
For more details, please refer to the following links:
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
None
0 Points
1 Post
Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Xml.XmlCDataSection
Oct 27, 2015 01:51 AM|jagadishnpy|LINK
<ClarificationComments><![CDATA[h]]></ClarificationComments>
<RequestDate><![CDATA[9/23/2015 12:00:00 AM]]></RequestDate>
<Department><![CDATA[]]></Department>
I am trying to deserialize this but whenever CDATA is null then my asmx web service crashes.If the CDATA is not null then i could make it work.
Please help
deserialize NET xml sharepointCSOM
All-Star
45489 Points
7008 Posts
Microsoft
Re: Unable to cast object of type 'System.Xml.XmlElement' to type 'System.Xml.XmlCDataSection
Oct 27, 2015 10:27 PM|Zhi Lv - MSFT|LINK
Hi jagadishnpy,
Welcome to asp.net forum.
As for this issue, I suggest you could check the object class. When you set the property, you need to check whether it is null. You could refer to the following code:
For more details, please refer to the following links:
https://social.msdn.microsoft.com/Forums/en-US/6f59a2c0-2f70-4f19-9210-c675bbdce48d/cdata-serialization-with-xmlserializer?forum=asmxandxml
http://stackoverflow.com/questions/9673030/deserialize-xml-with-empty-elements
http://www.codeproject.com/Articles/483055/XML-Serialization-and-Deserialization-Part
Best regards,
Dillion
deserialize NET xml sharepointCSOM