Reputation: 101
###################
Update: thanks to user2622016 I relized the ">" is valid .
Now , in my c# code I have a class that one of its fields has ">" as its value.
I want to serialize the class to xml WITHOUT the ">" being escaped to >
.
I am using xmlSerializer class. My code is:
memoryStream = new System.IO.MemoryStream();
System.Xml.XmlWriterSettings xmlWriterSettings = new System.Xml.XmlWriterSettings();
xmlWriterSettings.Encoding = encoding;
xmlWriterSettings.Indent = true;
System.Xml.XmlWriter xmlWriter = XmlWriter.Create(memoryStream, xmlWriterSettings);
Serializer.Serialize(xmlWriter, myObj);
any Idea How to achieve that without writing the entire XML using "writeRaw" (since i want the entire class to be serialized) ?
###################
is ">" a valid character in XML ?
simple answer is no . based on everything I know.. (also, Invalid Characters in XML )
but when I am trying to check with xml validation tools available online like: http://www.w3schools.com/xml/xml_validator.asp , http://www.xmlvalidation.com/
it says the following xml is valid:
<?xml version="1.0" encoding="utf-8"?>
<object>
<innerObj attrib="myAttrib">invalid char is > why valid</innerObj >
</object>
How is it possible ?
(Reason i am asking , I am trying to take this xml as a class in c# and serialize it . obviously it escapes ">" to ">"
. and I don't want it to happen ..
Any explanation about this "Valid invalid" Character and how to solve my serialization issue ? )
Thanks!!!
Upvotes: 0
Views: 532
Reputation: 6423
Only ampersand (&) and the left angle bracket (<) characters cannot be used in text. The > 'greater than' is absolutely OK in xml text fields, because it does not make it ambiguous. See chapter 2.4 http://www.w3.org/TR/2008/REC-xml-20081126/#syntax
The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings " & " and " < " respectively. The right angle bracket (>) may be represented using the string "
>
", and must, for compatibility, be escaped using either ">
" or a character reference when it appears in the string " ]]> " in content, when that string is not marking the end of a CDATA section.
Why C# always escapes it as >
? I don't know, maybe for some compatibility with some previous implementations?
According to the standard implementor may escape > as >
if he wants to, but is required to do so only in combination ]]>
Upvotes: 2
Reputation: 463
You can use CDATA. Try this
<?xml version="1.0" encoding="utf-8"?>
<object>
<innerObj attrib="myAttrib">invalid char is <![CDATA[>]]> why valid</innerObj >
</object>
Upvotes: 1