Reputation: 3870
I'm having problems while attaching some strings to some XML nodes. To explain this better I've made a simple example... just imagine that I have this XML code:
<song>
<title>
Surfin' USA
</title>
</song>
Please notice that instead of simply writing "Surfin' USA" I'm expressly using '
instead of the single quote character.
The XML code is valid - or at least it has been considered valid by some tools I have found and tested online.
My Problem is that when I try to put that string into a XmlText object, this way:
Dim xmlDoc As New XmlDocument()
Dim xmlMyText As XmlText
xmlMyText = xmlDoc.CreateTextNode("Surfin' USA")
I would expect to have the very same string value inside the XmlText object, but instead it's getting a slightly different value:
MsgBox(xmlMyText.OuterXml) ' OUTPUTS: Surfin&39; USA
In short, the ampersand symbol gets converted into &
because AFAIK "an ampersand cannot be left unescaped". But in my opinion in this case it is not unescaped because it is used in conjunction with #39 and the semicolon, am I wrong ?
Could you help me and explain how could I avoid this conversion?
Thank you very much for your time and help
Upvotes: 1
Views: 658
Reputation: 16281
It is a common problem to have a mismatch between the number of escaping passes you have sent in and how many you are trying to use on the data being output.
Here the OuterXml is doing exactly what it is designed to do: giving you the data it provided in an escaped (XML as stored) form. This is a good thing, because if you save this XML and then load it back into an XML document, you can navigate to the node and ask for .InnerText() on the node and get the original value, which is what you would expect. For it to do otherwise would break the contract.
However, by asking for OuterXml (which returns markup) instead of getting the InnerText value (which returns stored data), you are "off by one" on the escaping sequence. You can manually run an unescaping on the data or use the .InnerText() to automatically convert back to your source data.
using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Xml;
namespace TestProject1
{
[TestClass]
public class UnitTest1
{
private const string testString = "Surfin' USA";
[TestMethod]
public void TestMethod1()
{
XmlDocument xmlDoc = new XmlDocument();
XmlText xmlMyText;
xmlMyText = xmlDoc.CreateTextNode(testString);
Assert.AreEqual(testString, xmlMyText.InnerText);
}
}
}
This unit test passes. Please forgive my use of C#, but that is what my fingers type.
Upvotes: 1