G
Guest
I would like to serialize some XML where one of the elements contains html
content:
<MyXML><DataContent><p><b>ABC News Online</b></p></DataContent><MyXML>
However the XMLSerializer gives me the following exception:
System.Xml.XmlException: Unexpected node type Element. ReadElementString
method can only be called on elements with simple or empty content
I would like the serializer to read out all the HTML content from the
<DataContent> element.
My class looks like:
using System.Xml.Serialization;
namespace ABC.MyXML_5.BL
{
[XmlRoot("MyXML")]
public class MyXML_5
{
private string element_datacontent;
[XmlElement]
public string DataContent
{
get { return element_datacontent; }
set { element_datacontent = value; }
}
}
}
My code looks like:
//10.Create some XML
StringBuilder l_strBldrXML = new StringBuilder();
l_strBldrXML.Append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
l_strBldrXML.Append("<MyXML>");
l_strBldrXML.Append("<DataContent>");
l_strBldrXML.Append("<p><b>ABC News Online</b></p>");
l_strBldrXML.Append("</DataContent>");
l_strBldrXML.Append("</MyXML>");
//20.Put XML into an in memory stream
Stream l_StreamTest =
new
MemoryStream(Encoding.GetEncoding("Windows-1252").GetBytes(l_strBldrXML.ToString()));
//30.Deserialize into instance of a class
XmlSerializer l_Serializer = new
XmlSerializer(typeof(ABC.MyXML_5.BL.MyXML_5));
ABC.MyXML_5.BL.MyXML_5 l_MyXMLDocument;
l_MyXMLDocument =
(ABC.MyXML_5.BL.MyXML_5)l_Serializer.Deserialize(l_StreamTest);
l_StreamTest.Close();
Assert.IsTrue(l_MyXMLDocument.DataContent == "<p>ABC News Online</p>");
content:
<MyXML><DataContent><p><b>ABC News Online</b></p></DataContent><MyXML>
However the XMLSerializer gives me the following exception:
System.Xml.XmlException: Unexpected node type Element. ReadElementString
method can only be called on elements with simple or empty content
I would like the serializer to read out all the HTML content from the
<DataContent> element.
My class looks like:
using System.Xml.Serialization;
namespace ABC.MyXML_5.BL
{
[XmlRoot("MyXML")]
public class MyXML_5
{
private string element_datacontent;
[XmlElement]
public string DataContent
{
get { return element_datacontent; }
set { element_datacontent = value; }
}
}
}
My code looks like:
//10.Create some XML
StringBuilder l_strBldrXML = new StringBuilder();
l_strBldrXML.Append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
l_strBldrXML.Append("<MyXML>");
l_strBldrXML.Append("<DataContent>");
l_strBldrXML.Append("<p><b>ABC News Online</b></p>");
l_strBldrXML.Append("</DataContent>");
l_strBldrXML.Append("</MyXML>");
//20.Put XML into an in memory stream
Stream l_StreamTest =
new
MemoryStream(Encoding.GetEncoding("Windows-1252").GetBytes(l_strBldrXML.ToString()));
//30.Deserialize into instance of a class
XmlSerializer l_Serializer = new
XmlSerializer(typeof(ABC.MyXML_5.BL.MyXML_5));
ABC.MyXML_5.BL.MyXML_5 l_MyXMLDocument;
l_MyXMLDocument =
(ABC.MyXML_5.BL.MyXML_5)l_Serializer.Deserialize(l_StreamTest);
l_StreamTest.Close();
Assert.IsTrue(l_MyXMLDocument.DataContent == "<p>ABC News Online</p>");