I have a class that I need to serialize to XML with a specific format for a WCF service I'm using. The class structure is this:
[XmlRoot(Namespace="http://schemas.datacontract.org/2004/07/XXX.IT.Messaging.Common.Entities")]
public class PushBroadcastingParams
{
[XmlElement]
public string appId { get; set; }
[XmlElement]
public string message { get; set; }
[XmlArray(Namespace = "http://schemas.datacontract.org/2004/07/XXX.IT.Messaging.Common.Entities.KMS.Requests")]
public List<CustomKeyValuePair> customData { get; set; }
}
[XmlRoot(Namespace = "http://schemas.datacontract.org/2004/07/XXX.IT.Messaging.Common.Entities")]
public class PushNotificationParams : PushBroadcastingParams
{
[XmlArray(Namespace = "http://schemas.microsoft.com/2003/10/Serialization/Arrays")]
public string[] subscribersIDs { get; set; }
}
This is my serialization code:
public static string SerializeXML<T>(T obj, XmlSerializerNamespaces namespaces = null)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", "");
using (StringWriter sw = new StringWriter())
{
using (XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings { OmitXmlDeclaration = true }))
{
new XmlSerializer(typeof(T)).Serialize(writer, obj, ns);
return sw.ToString();
}
}
}
I need the XML output to be exactly like this:
<PushNotificationParams xmlns="http://schemas.datacontract.org/2004/07/XXX.IT.Messaging.Common.Entities" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<appId>123</appId>
<customData xmlns:orb="http://schemas.datacontract.org/2004/07/XXX.IT.Messaging.Common.Entities.KMS.Requests">
<orb:CustomKeyValuePair>
<orb:Key>HEADER</orb:Key>
<orb:Value>FOCUS</orb:Value>
</orb:CustomKeyValuePair>
<orb:CustomKeyValuePair>
<orb:Key>TITLE</orb:Key>
<orb:Value>title of message</orb:Value>
</orb:CustomKeyValuePair>
<orb:CustomKeyValuePair>
<orb:Key>DESC</orb:Key>
<orb:Value>desc of message</orb:Value>
</orb:CustomKeyValuePair>
<orb:CustomKeyValuePair>
<orb:Key>MESSAGE_TYPE</orb:Key>
<orb:Value>POD</orb:Value>
</orb:CustomKeyValuePair>
</customData>
<message>test</message>
<subscribersIDs xmlns:orb="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<orb:string>USER-NAME</orb:string>
</subscribersIDs>
</PushNotificationParams>
But I can't get it to be with the name space and the prefix exectly like this and I can't get rid of the xmlns:xsd
namespace.