473,769 Members | 4,173 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 6861
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 ReplaceXMLWrite r: XmlTextWriter
{
public ReplaceXMLWrite r(string filename, Encoding encoding)
: base(filename, encoding)
{
}
public override void WriteCData(stri ng text)
{
string oldText = text;

Console.WriteLi ne("ReplaceXMLW riter+WriteCDat a+oldText:{0}",
oldText);

base.WriteStrin g(HttpUtility.H tmlEncode( text));
}
}
=============== =============== ====
You can simply use your custom writer class as below:

=============== =====
static void Run()
{
XmlDocument doc = new XmlDocument();
doc.Load(@"..\. .\data.xml");
ReplaceXMLWrite r rxw = new ReplaceXMLWrite r("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(stri ng text)
{
string oldText = text;

Console.WriteLi ne("ReplaceXMLW riter+WriteCDat a+oldText:{0}",
oldText);

base.WriteStrin g(HttpUtility.H tmlEncode( 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.WriteStrin g(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
1827
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 there some way of doing this without repeating the try..except blocks ad-infinitum to handle 3D, 4D arrays etc. I can see there is a pattern here but I can't seem to turn it into a loop. Would a recursive solution do the trick?
0
2086
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 XSLT processing model: http://www.w3.org/TR/xslt#section-Processing-Model the root node will be processed by a built-in rule, because you do not
2
1773
by: Jimmy zhang | last post by:
Are they teh same thing in xml 1.0?
2
6945
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
6193
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> <tag><]></tag>
11
6467
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); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
1
4032
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 on that element to allow setting that inner text. After I create the object hierarchy and specify all the appropriate elements, attributes, etc., I serialize the objects to a .xml file using XmlSerializer. If I simply specify the...
4
9091
by: kplkumar | last post by:
After serialzing I want, <MyRequest> <Content SigningScheme="pkcs7"><!]></Content> </MyRequest> _________________________________________________________________________________________ My class looks like,
7
3980
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
758
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 create DOM-object, i get the value of "Data" without "\r\n"
0
10222
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10050
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9866
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.