0

###################

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 "&gt;" . and I don't want it to happen ..

Any explanation about this "Valid invalid" Character and how to solve my serialization issue ? )

Thanks!!!

Community
  • 1
  • 1
nati
  • 91
  • 2
  • 8
  • Why you dont want it to be replaced? [Is is recommended to do that](http://www.w3schools.com/xml/xml_syntax.asp)(see Entity References part) – Renatas M. Jan 05 '15 at 08:38
  • "and I don't want it to happen" ... when you don't want valid XML, use something else. – H H Jan 05 '15 at 08:40
  • look at @user2622016 answer , this is valid . it is on of the fields in my class and i want it to stay ">" when serialzing it . – nati Jan 05 '15 at 08:50
  • Still cant understand the problem. When you serializing `>` it will be replaced to `>` in your xml. When you deserialize it you will get `>` back. You want to see in xml `>`? But what you will do when you have `<>`? – Renatas M. Jan 05 '15 at 09:04
  • **The right angle bracket (>) may be represented using the string "`>`". Period.** It is up to the reader to interpret correctly both `>` and `>` as just `>`. You want for aesthetic reasons it to be as is in plain text, right? – user2622016 Jan 05 '15 at 09:11
  • Maybe post another question on StackOverflow - *How to disable escaping > right angle bracket in C#*. Simply I don't know how. – user2622016 Jan 05 '15 at 09:16
  • You've said you want ">" not to be escaped, but you haven't said why. If there's a downstream process that can handle ">" but can't handle ">", then that process is broken and needs fixing. – Michael Kay Jan 05 '15 at 09:53

2 Answers2

2

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 "&gt;", and must, for compatibility, be escaped using either "&gt;" 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 &gt? I don't know, maybe for some compatibility with some previous implementations?

According to the standard implementor may escape > as &gt if he wants to, but is required to do so only in combination ]]>

user2622016
  • 6,060
  • 3
  • 32
  • 53
  • Since it is valid , like you said. i want it to be written like it is and not be escaped , it is escaped when i am using the xmlSerializer class to serialize it from class object to xml. code: 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); – nati Jan 05 '15 at 08:46
1

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>
mayowa ogundele
  • 475
  • 2
  • 6
  • 19