Groups | Blog | Home
all groups > dotnet xml > april 2005 >

dotnet xml : InnerXML and InnerText BOTH don't work right


kben
4/20/2005 2:36:56 PM
I'm trying to get some text into an XMLElement that may or may not
contain markup, and it may or may not contain characters that need
escaping. I want the special characters escaped, but I want to preserve
the markup. This is so a user can enter some text with (xml
compatible) HTML tags and have it render properly (via XSLT) without
having to know about escaping the ampersand. For example, I want to
take the text:

"Nice t-shirt <p/> Cool & Stylish"

And put it in an element as such:

<ItemDescription>
Nice t-shirt <p/> Cool &amp; Stylish
</ItemDescription>

InnerXML chokes on the ampersand, and innerText escapes the tags. How
can I do this? I'm "manually" escaping the ampersand (and
apostrophies) in my code right now, and using innerXML, but it seems
like this must be a very common thing to want to do, and doing it
manually is error prone and more complex than it needs to be,
especially if the text might also have < or > that are not parts of
tags. I can't find any other properties on XMLElement that will do
this. There must be a way to do it directly.

--Kyle Bennett
Robbe Morris [C# MVP]
4/25/2005 7:33:19 PM
CDATA tags

--
2005 Microsoft MVP C#
Robbe Morris
http://www.robbemorris.com
http://www.masterado.net/home/listings.aspx



[quoted text, click to view]

Oleg Tkachenko [MVP]
5/1/2005 12:00:00 AM
[quoted text, click to view]

Sorry, but that's oxymoron. Text is text and markup is markup, you can't
have either-text-or-markup.

[quoted text, click to view]

This is looks like malformed XML or HTML. Whoever generates it - you
better get it fixed.

[quoted text, click to view]

That's by design. InnerXML expects XML and InnerText - text.
You can try reading such malformed XML via SgmlReader to get it
wellformed and then appends to XmlElement as XML.

--
Oleg Tkachenko [XML MVP, MCP]
AddThis Social Bookmark Button