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>
_x005C_MyData_x003A_Pool1is 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].