带有自定义命名空间的XmlSerializer

时间:2015-08-09 04:26:09

标签: c# xmlserializer

我想使用xml来使用一个安静的Web服务。我必须创建此模板才能创建有效的请求:

<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版本格式错误。我还发现了thisthisthis条款,但不是这些文章可以解决我的问题。

如何创建有效请求?

1 个答案:

答案 0 :(得分:2)

我们走了:

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