473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Converting XML with CDATA -> HTML

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.
Feb 6 '07 #1
6 6844
BillAtWork wrote:
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
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.

--
Oleg Tkachenko [XML MVP, MCPD]
http://blog.tkachenko.com | http://www.XmlLab.Net | http://www.XLinq.Net
Feb 6 '07 #2
BillAtWork wrote:
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?

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 6 '07 #3
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+old Text:{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/subscripti...ult.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/subscripti...t/default.aspx.

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

This posting is provided "AS IS" with no warranties, and confers no rights.

Feb 7 '07 #4
Steven Cheng[MSFT] wrote:
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)
{
string oldText = text;

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

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.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 7 '07 #5
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.
Feb 8 '07 #6
I'm just getting back to this issue now - thanks for your help - this looks
really good.

"Steven Cheng[MSFT]" wrote:
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.
Feb 17 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Simon Foster | last post by:
I have some code which attempts to convert Python arrays (tuples of tuples of tuples...) etc. into C arrays with equivalent contents. The prototype code is shown below. My only question is, is...
0
by: Dimitre Novatchev | last post by:
You seem to be unaware of the xslt processing which uses the built-in rules in the absence of templates that match some selected node. http://www.w3.org/TR/xslt#built-in-rule According to the...
2
by: Jimmy zhang | last post by:
Are they teh same thing in xml 1.0?
2
by: indo3 | last post by:
I know one can define CDATA text by <!]> which is the same as PCDATA: &lt;bla&gt; &amp;bla; But if you define an attribute in a DTD
3
by: Balaras | last post by:
Hi, Can sombody here please help me a bit with a regular expression. I have a xml file where I need to strip the CDATA sections of any contained data. Eg. <xml> <tag><]></tag>...
11
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString);...
1
by: soccerdad | last post by:
I've got a class hierarchy generated from a .xsd schema file using the XSD.EXE tool. One of the elements will have its "inner text" set to a CDATA block. The XSD.EXE tool exposed a "Value" property...
4
by: kplkumar | last post by:
After serialzing I want, <MyRequest> <Content SigningScheme="pkcs7"><!]></Content> </MyRequest> _________________________________________________________________________________________ ...
7
by: Max | last post by:
Hello everyone! Can anyone help me to convert the CDATA expression "CDATA ::= (Char* - (Char* ']]>' Char*)" to Javascript Regular Expression? Thanks, Max
18
by: sim.sim | last post by:
Hi all. i'm faced to trouble using minidom: #i have a string (xml) within CDATA section, and the section includes "\r\n": iInStr = '<?xml version="1.0"?>\n<Data><!]></Data>\n' #After i...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.