0

I read here and here that '/' is a valid xml character.
So,I have the following Controller/Models

using System.Runtime.Serialization;
using System.Web.Http;

namespace WebApplication1.Controllers
{
    public class ValuesController : ApiController
    {
        public Parent Get()
        {
            return new Parent() { Child = new Child() { Property1 = "222" } };
        }
    }

    [DataContract(Name = "MyName", Namespace = "")]
    public class Parent
    {
        [DataMember(Name = "Header/Footer", EmitDefaultValue = true)]
        public Child Child { get; set; }
    }

    [DataContract(Name = "MyName", Namespace = "")]
    public class Child
    {
        [DataMember(Name = "Property1", EmitDefaultValue = true)]
        public string Property1 { get; set; }

    }
}

The above GET actions returns the following (xml)

<MyName xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Part1_x002F_Part2>
        <Property1>222</Property1>
    </Header_x002F_Footer>
</MyName>

Can I somehow get "Part1/Part2" instead of "Part1_x002F_Part2"?
If I request the object as JSON,it works as expected

Community
  • 1
  • 1
George Vovos
  • 7,563
  • 2
  • 22
  • 45

1 Answers1

2

/ is a "valid" XML character, in that it may occur in text nodes without escaping. But / isn't valid in names, per the XML standard:

NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]

NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]

Name ::= NameStartChar (NameChar)*

/ is #x2F, in case you're wondering, so you can see it's not included in any range. This is deliberate:

The ASCII symbols and punctuation marks, along with a fairly large group of Unicode symbol characters, are excluded from names because they are more useful as delimiters in contexts where XML names are used outside XML documents; providing this group gives those contexts hard guarantees about what cannot be part of an XML name.

Jeroen Mostert
  • 27,176
  • 2
  • 52
  • 85
  • Good answer except for the final paragraph. The restriction on colon at the start of the name appears in the XML Namespaces specification, which most parsers choose to conform to. There are no similar restrictions on other characters that I am aware of, either in the specifications or in particular implementations. – Michael Kay Jul 20 '16 at 11:29
  • @MichaelKay: the XML spec actually contains a paragraph on that which I misinterpreted -- the wording is slightly misleading. It still makes no sense to include the colon in the *start* character, but hey. I've removed my paragraph since it's no more than an irrelevant aside. – Jeroen Mostert Jul 20 '16 at 11:41
  • I just have to say this: @George, I have been saying this all morning but you did not like it :) – Ares Jul 20 '16 at 20:53