C# 具有自定义命名空间的XmlSerializer

C# 具有自定义命名空间的XmlSerializer,c#,xmlserializer,C#,Xmlserializer,我想使用带有xml的restful web服务。我必须创建此模板才能创建有效的请求: <WS_IN_GetAccountCredit xmlns="http://schemas.datacontract.org/2004/07/WcfWebService"> <GetAccountCreditParams> <Password>String content</Password> <UserName>String co

我想使用带有xml的restful 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版本的格式错误。我还发现,他们中的任何一个都可以解决我的问题

如何创建有效的请求?

我们开始:

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

使用
通过
使用
通过

你能展示一下
WS_-IN_-GetAccountCredit
的样子吗?是的,诅咒在这里:public class WS_-IN_-GetAccountCredit{private WS_-IN_-WebServiceIdentity wsIdentity;private WS_-IN_-GetAccountCreditParams-GetAccountCreditParams;public WS_-IN-IN_-WebServiceIdentity wsIdentity{set{this.wsIdentity=value;}get{return this.wsIdentity;}}}public WS_IN_GetAccountCreditParams GetAccountCreditParams{set{this.GetAccountCreditParams=value;}get{return this.GetAccountCreditParams;}您也可以使用
xmlement
s
Order
属性来设置顺序。但是,既然这显然是一个WCF web服务,那么您就不能为它创建一个客户机,然后在WCF客户机类上调用该方法作为普通的C#方法吗?为什么需要“手工组装”XML???我当然能做到。但出于某些原因,我不得不将其作为restful服务使用。