0

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.

Liran Friedman
  • 4,027
  • 13
  • 53
  • 96
  • possible duplicate of [Omitting all xsi and xsd namespaces when serializing an object in .NET?](http://stackoverflow.com/questions/625927/omitting-all-xsi-and-xsd-namespaces-when-serializing-an-object-in-net) – Sinatr Aug 03 '15 at 14:05
  • 2
    Are you passing this data to someone's hand-rolled "XML is just fancy strings" parsing code? Because namespace *prefixes* shouldn't matter. `` and `` have *exactly* the same meaning, from an XML perspective. So if you need to exactly match prefixes, or avoid including additional prefixes, it means that someone's not using a library that understands what XML actually *is*. – Damien_The_Unbeliever Aug 03 '15 at 14:10
  • @Sinatr This is not duplicated because I only need to remove the `xmlns:xsd` and not all the namespaces – Liran Friedman Aug 03 '15 at 14:22
  • @Damien_The_Unbeliever: I don't know whay the prefix matters, but the fact is that it does – Liran Friedman Aug 03 '15 at 14:24
  • One possible solution is pre-/post- processing of generated xml. To do something manually with unwanted content. Or, well, generate it manually [from start](http://stackoverflow.com/q/11492705/1997232) (without using DTO and `XmlSerializer`). – Sinatr Aug 03 '15 at 14:26
  • I know, but I wanted to avoid manually building the XML... – Liran Friedman Aug 03 '15 at 14:29
  • Shouldn't prefixes be unique? In this example customData and subscribersIDs are using the same prefix for different namespaces. Is that how it's supposed to be? – Volkan Paksoy Aug 03 '15 at 15:18
  • Why is your class name PushBroadcastingParams but the tagname PushNotificationParams? – jdweng Aug 03 '15 at 17:07
  • @VolkanPaksoy - namespace prefixes are valid from the start of the element that defines them until the matching end element. There are two declarations of an `orb` prefix but they occur on two siblings. Even if they were nested, that would also be valid - the inner nested scope would apply until that element ended. The only real issue would be if you tried to declare the same prefix twice on the same element - that wouldn't be valid. – Damien_The_Unbeliever Aug 04 '15 at 05:24
  • @Damien_The_Unbeliever I see, thank you for the clear explanation. – Volkan Paksoy Aug 04 '15 at 06:53

0 Answers0