0

While serialising the object to the XML I use the attribute convention like [XmlElement("MyData:Pool1")] for me it does the job but the XML looks like

<_x005C_MyData_x003A_Pool1 >

I presume it convert to the colon or any special chars to some other formats, I tried changing with backslash, @ and $ signs preceding to the string, but it didn't helped me.

Any suggestions apart from using string / regex replace ?

  private void Serlise(Interface request)
    {
        var xsSubmit = new XmlSerializer(typeof(Interface));
        var ns = new XmlSerializerNamespaces();
        ns.Add("", "");
        var xml = "";

        using (var encoder = new CustomEncoder())
        {
            using (var  writer = XmlWriter.Create(encoder))
            {
                xsSubmit.Serialize(writer, request,ns);
                xml = encoder.ToString();
            }
        }
        File.WriteAllText(@"output.xml", xml);
    }

Below is the class to be serialised


[System.SerializableAttribute()]

[System.ComponentModel.DesignerCategoryAttribute("code")] [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo1.com/bar/test/")] [System.Xml.Serialization.XmlRootAttribute(ElementName = "Pool", Namespace = "http://www.foo1.com/bar/test/", IsNullable = false)] public partial class Root {

private Tester adminField;

private string versionField;

[XmlElement("Test:Pool1")]
public Tester Admin
{
    get
    {
        return this.adminField;
    }
    set
    {
        this.adminField = value;
    }
}

 
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Version
{
    get
    {
        return this.versionField;
    }
    set
    {
        this.versionField = value;
    }
}

}

NOTE : Slightly adjusted namespace & class name on XML due to NDA

<?xml version="1.0" encoding="utf-8"?><q1:MydataPool Version="1" xmlns:q1="http://www.foo1.com/bar/test"><q1:Data Id ="000123" Function="Hi"><q1:Hello Test="Abcd"  /></q1:Data></q1:MydataPool>
11
  • _x005C_MyData_x003A_Pool1 is a valid XML name. Have a look here at the W3 specs. There you will see that an XML Name can (only) start with an NameStartChar like ":" | [A-Z] | "_" | [a-z] | ... followed by additional NameStartChars and NameChars like "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]. Commented Aug 18, 2021 at 22:14
  • 1
    This is a common convention for generating a valid element name from a string that contains characters that aren't allowed in an element name. What exactly is your problem with it? It isn't pretty, but it's a sensible way around the restrictions. Commented Aug 18, 2021 at 22:39
  • 1
    Probably you should use a namespace with the MyData prefix? Commented Aug 18, 2021 at 22:48
  • @AlexanderPetrov, I tried with namespace but no luck. Commented Aug 19, 2021 at 12:28
  • @MichaelKay, my real problem is the API is validating the kind of XML chunk I pass to it. It actively reject the one with strange chars like x003A . since I have no control over the API I had to stick with their conventions. Commented Aug 19, 2021 at 12:30

1 Answer 1

1

Specify the namespace in the property attribute:

[XmlElement("Pool1", Namespace = "url")]
public Tester Admin

Set a prefix for this namespace:

var ns = new XmlSerializerNamespaces();
ns.Add("MyData", "url");

In the result, you will get

<MyData:Pool1>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.