all groups > dotnet xml > may 2007 >
You're in the

dotnet xml

group:

Is there XmlDecode/XmlEncode?



Is there XmlDecode/XmlEncode? Fallen
5/23/2007 11:46:19 PM
dotnet xml: I have a string containing XML embedded withing another XML document
as an attribute value. This is what the string looks like:
<message>quick brown fox&ltmessage/>

I want to convert this to: <message>quick brown fox</message>

How?
(I don't think XmlConvert handles this.)

regards,
T
Re: Is there XmlDecode/XmlEncode? John Saunders [MVP]
5/24/2007 12:00:00 AM
[quoted text, click to view]

What happens if you do this:

XmlDocument doc = new XmlDocument();
doc.LoadXml(myWholeDocument);
// Assuming <child attributeWithXML="&lt;message&gt;quick brown
fox&ltmessage/&gt;"/>
string embeddedXml =
doc.DocumentElement["child"].Attributes["attributeWithXML"].Value;
Console.WriteLine(embeddedXml);

What prints out?
--
John Saunders [MVP]

Re: Is there XmlDecode/XmlEncode? T
5/24/2007 4:51:16 AM
Thanks John,

The following code does indeed print <message>quick brown fox</
message>.
But I know exactly where in the containing XML this string occurs. I
am able to extract this without the expense of parsing. I am looking
for a shortcut to unescape the embedded XML. But thanks the answer is
obvious from here.

[code]
using System;
using System.Xml;

class Starter {
public static void Main(string[] args){
XmlDocument doc = new XmlDocument();
string xml = "<d><child attributeWithXML=\"&lt;message&gt;quick
brown fox&lt;/message&gt;\" /></d>";
doc.LoadXml(xml);
string embeddedXml =
doc.DocumentElement["child"].Attributes["attributeWithXML"].Value;
Console.WriteLine(embeddedXml);
}
}
[/code]

On May 24, 4:11 pm, "John Saunders [MVP]" <john.saunders at
[quoted text, click to view]
Re: Is there XmlDecode/XmlEncode? John Saunders [MVP]
5/24/2007 1:21:26 PM
[quoted text, click to view]

Glad to help.

The XmlDocument example was just a simple example to illustrate the fact
that the XML API's know how to escape and unescape XML. If you're only
looking for the one string, then an XPathDocument or even a simple XmlReader
would be much faster. Both of them understand how strings need to be
escaped.
--
John Saunders [MVP]

AddThis Social Bookmark Button