Mohammad
Mohammad

Reputation: 2764

XmlSerializer with custom namespace

I want to consume a restful web service with xml. I have to create this template in order to create a valid request:

<WS_IN_GetAccountCredit xmlns="http://schemas.datacontract.org/2004/07/WcfWebService">
  <GetAccountCreditParams>
    <Password>String content</Password>
    <UserName>String content</UserName>
  </GetAccountCreditParams>
  <WSIdentity>
    <WS_PassWord>String content</WS_PassWord>
    <WS_UserName>String content</WS_UserName>
  </WSIdentity>
</WS_IN_GetAccountCredit>

My serializer method is like this:

XmlSerializerNamespaces xmlNameSpace = new XmlSerializerNamespaces();
xmlNameSpace.Add("", "http://schemas.datacontract.org/2004/07/WcfWebService");

XmlSerializer xmlSerializer = new XmlSerializer(instance.GetType());

using (StringWriter textWriter = new StringWriter())
{
    xmlSerializer.Serialize(textWriter, instance, xmlNameSpace); ; return textWriter.ToString();
}

My output looks like this:

<?xml version="1.0" encoding="utf-16"?>
<WS_IN_GetAccountCredit xmlns:xmlns="http://schemas.datacontract.org/2004/07/WcfWebService">
  <WSIdentity>
    <WS_UserName>String content</WS_UserName>
    <WS_PassWord>String content</WS_PassWord>
  </WSIdentity>
  <GetAccountCreditParams>
    <UserName>String content</UserName>
    <Password>String content</Password>
  </GetAccountCreditParams>
</WS_IN_GetAccountCredit>

As you can see, the namespace and xml version is in wrong format. I also found this, this and this article but non of them could solve my problem.

How can create a valid request?

Upvotes: 2

Views: 719

Answers (1)

Amit Kumar Ghosh
Amit Kumar Ghosh

Reputation: 3726

Here we go:

    [XmlRoot(ElementName = "WS_IN_GetAccountCredit", Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]
    public class WS_IN_GetAccountCredit
    {
        private WS_IN_WebServiceIdentity wsIdentity;
        private WS_IN_GetAccountCreditParams getAccountCreditParams;
        public WS_IN_WebServiceIdentity WSIdentity { set { this.wsIdentity = value; } get { return this.wsIdentity; } }
        public WS_IN_GetAccountCreditParams GetAccountCreditParams
        {
            set { this.getAccountCreditParams = value; }
            get { return this.getAccountCreditParams; }
        }


    }
    public class WS_IN_WebServiceIdentity
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }

    public class WS_IN_GetAccountCreditParams
    {
        public string UserName { get; set; }
        public string Password { get; set; }
    }



            var namespaces = new XmlSerializerNamespaces();
            namespaces.Add("", "http://schemas.datacontract.org/2004/07/WcfWebService");
            var ser = new XmlSerializer(typeof(WS_IN_GetAccountCredit));
            using (var writer = new StringWriter())
            {
                ser.Serialize(writer, new WS_IN_GetAccountCredit
                {
                    GetAccountCreditParams = new WS_IN_GetAccountCreditParams { Password = "pass", UserName = "use" },
                    WSIdentity = new WS_IN_WebServiceIdentity { Password = "pass", UserName = "use" }
                },
                    namespaces);
                var xml = writer.ToString();
            }

The result is:

<?xml version="1.0" encoding="utf-16"?>
<WS_IN_GetAccountCredit xmlns="http://schemas.datacontract.org/2004/07/WcfWebService">
  <WSIdentity>
    <UserName>use</UserName>
    <Password>pass</Password>
  </WSIdentity>
  <GetAccountCreditParams>
    <UserName>use</UserName>
    <Password>pass</Password>
  </GetAccountCreditParams>
</WS_IN_GetAccountCredit>

Upvotes: 2

Related Questions