473,543 Members | 4,407 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 28100
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
7208
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
1240
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
3819
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
1075
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
3178
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
3450
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
1136
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
7619
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
7401
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...
0
7344
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
7589
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. ...
1
7335
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...
1
5260
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
3387
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...
0
3391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1810
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
0
628
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...

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.