Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

XMLSerialization of complex content

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DirkGoes - 18 Oct 2006 09:53 GMT
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>");
Dave Sexton - 18 Oct 2006 13:31 GMT
Hi,

You have a couple of simple options:

1. CDATA
   <![CDATA[<p><b>ABC News Online</b></p>]]>
2. Xml Encoding
   &lt;p&gt;&lt;b&gt;ABC News Online&lt;/b&gt;&lt;/p&gt;

In either case your assert statement will pass, but only if you make a small adjustment first so it looks like this: "<p><b>ABC News
Online</b></p>" .

If you don't have control over the xml content then you should use an XmlDocument object or XmlReader first to go through every node
and fix the bad xml.  Your sample is, after all, bad xml if the p and b tags are not part of your valid schema definition.

Signature

Dave Sexton

>I would like to serialize some XML where one of the elements contains html
> content:
[quoted text clipped - 57 lines]
>
> Assert.IsTrue(l_MyXMLDocument.DataContent == "<p>ABC News Online</p>");

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.