I have to serialize the following object using ServiceStack.Text.XmlSerializer.Serialize removing all the attributes. I know that it's a bad practice but I've to dialog with an old c++ written server via simulated XML (it's parsed manually) and it doesn't handle the attributes and so on
My class is
[DataContract(Namespace = "", Name = "DataValutaRequest")]
public class DateValueRequestPayload
{
[DataMember()]
public int Cross { get; set; }
[DataMember()]
public DateTime TradeDate { get; set; }
}
and it got serialized to
<?xml version="1.0" encoding="utf-8"?><DataValutaRequest xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Cross>11</Cross><TradeDate>2015-07-27T00:00:00+02:00</TradeDate></DataValutaRequest>
I need to remove
<?xml version="1.0" encoding="utf-8"?>
xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
How can I do that? Thanks
The xmlns:i
XML Namespace is automatically emitted by .NET's DataContractSerializer and doesn't provide an option to omit it.
Only real way to remove it is to serialize to XML and then strip the attributes from the raw XML, e.g something like:
var xml = dto.ToXml()
.Replace(" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"","");
return xml;
If you have more complicated requirements you can also look at loading and removing XML with XDocument.