2

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?

Community
  • 1
  • 1
Mohammad
  • 2,724
  • 6
  • 29
  • 55
  • can you show how `WS_IN_GetAccountCredit ` looks like? – Amit Kumar Ghosh Aug 09 '15 at 05:31
  • yes of curse: here you are: 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; } } – Mohammad Aug 09 '15 at 05:38
  • you can also use `XmlElement`s `Order` property to set the sequence. – Amit Kumar Ghosh Aug 09 '15 at 05:53
  • 1
    But since this is **obviously** a WCF web service - can't you just create a **client** for it and then call that method as a normal C# method on the WCF client class?? Why do you need to "hand-assemble" the XML??? – marc_s Aug 09 '15 at 07:20
  • of course i can do it. but i have to consume it as restful service for some reasons. – Mohammad Aug 09 '15 at 07:36

1 Answers1

2

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>
Amit Kumar Ghosh
  • 3,618
  • 1
  • 20
  • 24