So I'm writing some XML generating code, and found that the following attribute value was screwing up the XML formatting:
"Jim/Bob"
So I looked into the XML Entities used as escape sequences and every list I saw did not include one for the forward slash. Am I missing something obvious here? Seems like the sort of thing you'd want to escape...
An entity reference is a group of characters used in text as a substitute for a single specific character that is also a markup delimiter in XML.
In general, we have three types of entities: internal entities, external entities, and parameter entities.
In contrast, a character entity reference refers to a character by the name of an entity which has the desired character as its replacement text. The entity must either be predefined (built into the markup language) or explicitly declared in a Document Type Definition (DTD).
As we said previously, an XML DTD is a collection of XML entity and element declarations and comments. Entities are name/value pairs that make the DTD easier to read and understand, while elements are the actual markup tags defined by the DTD, like HTML's <p> or <h1> tags.
The forward slash is valid as is and does not need further encoding.
The only reserved characters are:
> < & %
For even more XML entities - http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references
I know it turned out this wasn't the problem, but I thought it would be helpful to mention that, in addition to bobince's answer, the Fraction Slash HTML entity ⁄
looks just like a forward slash. Just in case anybody reaching this page actually does want an HTML entity for something representing a forward slash.
I don't think the comments in this thread are entirely correct since if you use a schema (XSD) you could define elements with names Jim, Bob and Jim/Bob without any issues. But then when you want to define the element and entity:
<names>
<Jim>Carrey</Jim>
<Bob>Jones</Bob>
<Jim/Bob>Six figured Hillbilly</Jim/Bob>
</names>
The problems are obvious.
FYI although a slash is valid XML, watch out how it's being used downstream. We used it for connecting to an azure service bus queue. Azure itself generated keys with slashes in the connection strings, but then broke silently when we tried to use them. Escaping them with “/”
made it work
"Jim/Bob"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With