469,328 Members | 1,260 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,328 developers. It's quick & easy.

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.OwnerDocument.CreateElement( nodeName );
nodeParent.AppendChild( nodeText );
nodeParent.Appendchild( nodeParent.OwnerDocument.CreateTextElement(
nodeValue ) );
return nodeText;
}

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

<Label>&lt;Actor0&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 25887
Richard Lewis Haggard wrote:
public static XmlNode AppendText( XmlNode nodeParent, string nodeName,
string nodeValue )
{
XmlNode nodeText = nodeParent.OwnerDocument.CreateElement( nodeName );
nodeParent.AppendChild( nodeText );
nodeParent.Appendchild( nodeParent.OwnerDocument.CreateTextElement(
nodeValue ) );
return nodeText;
}

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

<Label>&lt;Actor0&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.LoadXml("<Label>&lt;Actor0&gt;</Label>");
Console.WriteLine("InnerText: \"{0}\"\r\nInnerXml: \"{1}\"",
xmlDocument.DocumentElement.InnerText,
xmlDocument.DocumentElement.InnerXml);

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*******@yahoo.dewrote in message
news:OQ**************@TK2MSFTNGP03.phx.gbl...
Richard Lewis Haggard wrote:
>public static XmlNode AppendText( XmlNode nodeParent, string nodeName,
string nodeValue )
{
XmlNode nodeText = nodeParent.OwnerDocument.CreateElement(
nodeName );
nodeParent.AppendChild( nodeText );
nodeParent.Appendchild( nodeParent.OwnerDocument.CreateTextElement(
nodeValue ) );
return nodeText;
}

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

<Label>&lt;Actor0&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.LoadXml("<Label>&lt;Actor0&gt;</Label>");
Console.WriteLine("InnerText: \"{0}\"\r\nInnerXml: \"{1}\"",
xmlDocument.DocumentElement.InnerText,
xmlDocument.DocumentElement.InnerXml);

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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Christopher Pragash | last post: by
2 posts views Thread by Snowman | last post: by
3 posts views Thread by umbertoeklat | last post: by
reply views Thread by Miguel RS | last post: by
1 post views Thread by Philipp | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.