473,699 Members | 2,279 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 28194
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
7224
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. Thanks, Nimmi
0
1249
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <MetaData>MetaData</MetaData>
2
3824
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 element) without Parent property serialized, this may be done by adding XmlIgnoreAttribute on the...
0
1080
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
1801
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
3187
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 probably shouldn't use it. I need a way to escape text on the client, and then unescape it on the...
8
3463
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 server I can intercept the SOAP message (XML doc itself),
0
1140
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, containig attributes like name, age and so on and I want to show that info in the webpage. OK, the...
1
7626
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, "&amp;");
0
8700
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9048
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...
1
8931
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8892
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...
0
7767
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5878
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
4635
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2014
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.