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 / XML / February 2007

Tip: Looking for answers? Try searching our database.

Converting XML with CDATA -> HTML

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
BillAtWork - 06 Feb 2007 11:35 GMT
Hi,
I'm defining a report layout using an XML document, plugging data values
into fields before outputting the whole doc as an HTML page. I wrap each of
the data fields in CDATA section (in case the data contains reserved chars
like "&").

Currently, I perform a simple search-and-replace to strip out the CDATA tags
before outputting the document. It works but it's not as elegant or reliable
as I would wish. Is there a better way of performing this transition i.e.

XML doc with CDATA -> string with CDATA -> string without CDATA -> HTML

Thanks.
Oleg Tkachenko [MVP] - 06 Feb 2007 12:01 GMT
> Hi,
> I'm defining a report layout using an XML document, plugging data values
[quoted text clipped - 7 lines]
>
> XML doc with CDATA -> string with CDATA -> string without CDATA -> HTML

If you were using XML API to build your XML you wouldn't have to worry
about low-level XML syntax issues like escaping reserved characters with
CDATA. Both XmlDocument and XmlWriter handle it for you.

Signature

Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net

Martin Honnen - 06 Feb 2007 13:19 GMT
> Currently, I perform a simple search-and-replace to strip out the CDATA tags
> before outputting the document. It works but it's not as elegant or reliable
> as I would wish. Is there a better way of performing this transition i.e.
>
> XML doc with CDATA -> string with CDATA -> string without CDATA -> HTML

XSLT can transform XML to HTML. But I am not sure I understand exactly
what you are trying to achieve. Can you show us a sample XML input you
have and the HTML output you want to create from that?

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Steven Cheng[MSFT] - 07 Feb 2007 06:17 GMT
Hello Bill,

Based on your description, your main concern here is looking for a best
means to replace all the CData Sections in your report XML document with
normal text node(with some encoding), correct?

I've tried some xpath or XSLT approach, but seems doesn't quite suit the
scenario here. One means I've got is creating a custom XmlWriter and
override the "WriteCData" method so as to do our own customized output code
logic for CData node in XML Document.  here is a very simple custom writer
class demonstrate this:

===============================
public class ReplaceXMLWriter: XmlTextWriter
   {
       public ReplaceXMLWriter(string filename, Encoding encoding)
           : base(filename, encoding)
       {
       }

       public override void WriteCData(string text)
       {

           
           string oldText = text;

           Console.WriteLine("ReplaceXMLWriter+WriteCData+oldText:{0}",
oldText);

           base.WriteString(HttpUtility.HtmlEncode( text));
       }
   }
==================================

You can simply use your custom writer class as below:

====================
static void Run()
       {
           XmlDocument doc = new XmlDocument();
           doc.Load(@"..\..\data.xml");

           ReplaceXMLWriter rxw = new ReplaceXMLWriter("replaceddata.xml",
Encoding.UTF8);

           doc.Save(rxw);

           rxw.Close();
}
=========================================

Does this helps you?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
Martin Honnen - 07 Feb 2007 13:47 GMT
> Based on your description, your main concern here is looking for a best
> means to replace all the CData Sections in your report XML document with
> normal text node(with some encoding), correct?

> here is a very simple custom writer
> class demonstrate this:

>         public override void WriteCData(string text)
>         {
[quoted text clipped - 6 lines]
>
>             base.WriteString(HttpUtility.HtmlEncode( text));
                               ^^^^^^^^^^^^^^^^^^^^^^

Why are you using HtmlEncode instead of simply calling WriteString? With
your approach for instance an ampersand in a CDATA section (e.g.
<![CDATA[foo & bar]]> will then be escaped twice (e.g. foo &amp;amp;
bar)? Is that your intention? If you simply called
  base.WriteString(text)
then the XmlWriter takes care of escaping that as e.g. foo &amp; bar.

Signature

    Martin Honnen --- MVP XML
    http://JavaScript.FAQTs.com/

Steven Cheng[MSFT] - 08 Feb 2007 01:47 GMT
Hi Martin,

Thanks for your good suggestion. My previous code just demonstrate that we
can do some customization as we like here :).  Anyway, I did missed the
auto escaping of the WriteString method. Thanks again for your care.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
BillAtWork - 17 Feb 2007 10:58 GMT
I'm just getting back to this issue now - thanks for your help - this looks
really good.

> Hi Martin,
>
[quoted text clipped - 10 lines]
>
> This posting is provided "AS IS" with no warranties, and confers no rights.

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.