473,568 Members | 3,101 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Escape and unescape text for serialization and deserialization ?

Is it possible to use features from XmlDocument to unescape text back to its
original raw text format after it has been escaped to handle non-HTML
compliant character strings?

I have code that serializes text to an XML file and then deserializes back
to text. If the user enters XML illegal text like "<Actor1>", the code
properly escapes it to "&lt;Actor1&gt; ", something that doesn't interfere
with the XML syntax, and writes it into the Xml document, but the extracted
text from the node is not being 'unescaped' back to its original text. Is
there some way to use the built in features to restore the text data without
having to write yet another XML decoder parser?

Here's how the serialization works - Assuming that an XmlDocument object has
been created and it has some node already associated with it named
nodeParent, this is how a text node will be appended to that node.

public static XmlNode AppendText( XmlNode nodeParent, string nodeName,
string nodeValue )
{
XmlNode nodeText = nodeParent.Owne rDocument.Creat eElement( nodeName );
nodeParent.Appe ndChild( nodeText );
nodeParent.Appe ndchild( nodeParent.Owne rDocument.Creat eTextElement(
nodeValue ) );
return nodeText;
}

Assume that the text string "<Actor0>" is saved to node "Label". The result
in the XML file is

<Label>&lt;Acto r0&gt;</Label>

To get the data out, I'm (incorrectly) using the XmlNode's InnerText
property, which simply returns the serialized text as it was written to the
file instead of converting it back to the original text. Is there an XmlNode
function that will unescape the text, thus returning the original text?

--
Richard Lewis Haggard
www.Haggard-And-Associates.com
Feb 12 '07 #1
2 28119
Richard Lewis Haggard wrote:
public static XmlNode AppendText( XmlNode nodeParent, string nodeName,
string nodeValue )
{
XmlNode nodeText = nodeParent.Owne rDocument.Creat eElement( nodeName );
nodeParent.Appe ndChild( nodeText );
nodeParent.Appe ndchild( nodeParent.Owne rDocument.Creat eTextElement(
nodeValue ) );
return nodeText;
}

Assume that the text string "<Actor0>" is saved to node "Label". The result
in the XML file is

<Label>&lt;Acto r0&gt;</Label>

To get the data out, I'm (incorrectly) using the XmlNode's InnerText
property, which simply returns the serialized text as it was written to the
file instead of converting it back to the original text. Is there an XmlNode
function that will unescape the text, thus returning the original text?
Are you sure you are using InnerText? It sounds as if you have used
InnerXml.

Short test case

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml("<Label>&l t;Actor0&gt;</Label>");
Console.WriteLi ne("InnerText: \"{0}\"\r\nInne rXml: \"{1}\"",
xmlDocument.Doc umentElement.In nerText,
xmlDocument.Doc umentElement.In nerXml);

outputs

InnerText: "<Actor0>"
InnerXml: "&lt;Actor0&gt; "

so InnerText should give you what you want.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 12 '07 #2
Thanks!

You are correct. The error conditions were coming from those branches of
code that used InnerXml instead of InnerText.
--
Richard Lewis Haggard
www.Haggard-And-Associates.com

"Martin Honnen" <ma*******@yaho o.dewrote in message
news:OQ******** ******@TK2MSFTN GP03.phx.gbl...
Richard Lewis Haggard wrote:
>public static XmlNode AppendText( XmlNode nodeParent, string nodeName,
string nodeValue )
{
XmlNode nodeText = nodeParent.Owne rDocument.Creat eElement(
nodeName );
nodeParent.Appe ndChild( nodeText );
nodeParent.Appe ndchild( nodeParent.Owne rDocument.Creat eTextElement(
nodeValue ) );
return nodeText;
}

Assume that the text string "<Actor0>" is saved to node "Label". The
result
in the XML file is

<Label>&lt;Act or0&gt;</Label>

To get the data out, I'm (incorrectly) using the XmlNode's InnerText
property, which simply returns the serialized text as it was written to
the
file instead of converting it back to the original text. Is there an
XmlNode
function that will unescape the text, thus returning the original text?

Are you sure you are using InnerText? It sounds as if you have used
InnerXml.

Short test case

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Loa dXml("<Label>&l t;Actor0&gt;</Label>");
Console.WriteLi ne("InnerText: \"{0}\"\r\nInne rXml: \"{1}\"",
xmlDocument.Doc umentElement.In nerText,
xmlDocument.Doc umentElement.In nerXml);

outputs

InnerText: "<Actor0>"
InnerXml: "&lt;Actor0&gt; "

so InnerText should give you what you want.

--

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

Feb 12 '07 #3

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

Similar topics

12
7214
by: Nimmi Srivastav | last post by:
Are there any design patterns for object serialization/deserialization in C++? Where can I find information pertaining to them? Can someone post some real coding examples please? I have a need to serialize/deserialize objects that contain data fields and pointer fields. Furthermore, not all the fields need to be serialized/deserialized. ...
0
1243
by: Christopher Pragash | last post by:
Hello All, I'm trying to use XML Serialization to a class that inherits from another class and also has ArrayLists exposed as properties. When I serialize the class after populating the methods I get the following XML: <?xml version="1.0" encoding="utf-16"?> <Bulletin xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
2
3821
by: Snowman | last post by:
Suppose I have a RootObject which holds a collection of other objects. The other objects have a property (Parent) which refers back to the "parent" collection (b.t.w. my collection is based on CollectionBase), in similar fashion as the object models of MS Office. I want to serialize this object graph (with RootObject as the xml document...
0
1076
by: gavkel | last post by:
Hi all Got a problem with Serialization / Deserialization object model (originally built using XSD.exe then tidied up) and namespaces. Basically Its coming out like this... <blah xnls="blahblah"...> <blahblah> </blahblah>
3
1798
by: umbertoeklat | last post by:
as a proof of concept, am trying create a web app w/ an XML file as a temp DB What is the fastest way to do serialization/deserialization? or maybe the better question would be how to efficiently retrieve/save data to an xml file. have tried deserializing from XML file but having a hard time on mulitple records...
1
3181
by: Harry Keck | last post by:
Is there a set of methods in the .Net framework that do the same thing as the jscript functions escape and unescape? I get very close with HttpUtility.UrlEncodeUnicode, but it puts "+" for spaces instead of "%20". Uri.EscapeString seems to create the correct escape string, but there is no unescape. The function is not public anyway, so I...
8
3452
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a requirement to have a server web service class. (reasons below) 3. I want to develop something like this - when client makes a web service call, on the...
0
1139
by: Miguel RS | last post by:
Hi all, I have a winforms app (app1), a webservice (ws1) and a webpage (wp1). The webservice exposes a type (type1). When I generate a proxy (px1) for app1 I get the type ws1.type1. The thing is that I want to serialize an instance of this type, pass it as a parameter to wp1 and then deserialize it. Let's say that type1 is a person´s info,...
1
7620
by: Philipp | last post by:
How do I convert a string to escaped HTML? (Let's say I'm not using DOM.) E.g. "<p>foo</p>" would become "&lt;p&gt;foo&lt;/p&gt;"? Reversely, how do I unescape a string to be HTML? E.g. "&lt;em&gt;" would turn back into "<em>". I mean a function like the following would do the job but is there a more JS-native solution? html = html.replace(/&/g,...
0
7601
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7913
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. ...
0
8117
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...
1
7659
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7962
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...
1
5496
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...
0
5217
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.