具有自定义命名空间的 XmlSerializer

XmlSerializer with custom namespace

我想使用 restful Web 服务 xml。我必须创建此模板才能创建有效请求:

<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>

我的序列化方法是这样的:

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();
}

我的输出如下所示:

<?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>

如您所见,命名空间和 xml 版本格式错误。我还找到了 this, this and this 篇文章,但没有一篇可以解决我的问题。

如何创建有效请求?

开始吧:

    [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();
            }

结果是:

<?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>