繁体   English   中英

在服务堆栈中发送xml数组

[英]Sending an xml array in service stack

我目前正在使用ServiceStack执行以下操作,以将一些xml发布回服务器:

<Server xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <UserName>Bob</UserName>
    <UserGroups xmlns:d3p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
       <d3p1:string>History</d3p1:string>
       <d3p1:string>Geography</d3p1:string>
     </UserGroups>
</Server>

上面的作品,但是我怎么做为:

<Server xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <UserName>Bob</UserName>
    <UserGroups>
       <UserGroup>History</UserGroup>
       <UserGroup>Geography</UserGroup>
     </UserGroups>
</Server>

我努力了:

[CollectionDataContract(ItemName = "UserGroup")]
public partial class ArrayOfStringUserGroup : List<string>
{
    public ArrayOfStringUserGroup()
    {
    }

    public ArrayOfStringUserGroup(IEnumerable<string> collection) : base(collection) { }
    public ArrayOfStringUserGroup(params string[] args) : base(args) { }
}

而我在帖子中的dto具有以下内容:

  [DataMember(Name = "UserGroups", Order = 3)]
  public ArrayOfStringUserGroup UserGroups { get; set; }

但是我将UserGroups作为UserGroupDto的空数组获取。

这正是您想要的。

Server s = new Server();
s.UserName = "Bob";
s.UserGroups = new List<string>();
s.UserGroups.Add("History");
s.UserGroups.Add("Geography");


StringWriter stream = new StringWriter();
XmlWriter writer = 
            XmlTextWriter.Create(
              stream,
              new XmlWriterSettings() { OmitXmlDeclaration = true,Indent = true }
            );

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("i", "http://www.w3.org/2001/XMLSchema-instance");

XmlSerializer xml = new XmlSerializer(typeof(Server));
xml.Serialize(writer,s,ns);

var xmlString = stream.ToString();

public class Server
{
    public string UserName;
    [XmlArrayItem("UserGroup")]
    public List<string> UserGroups;
}

您是否只想删除冗余/重复的XML名称空间?

如果是这样,您应该确保所有DTO类型共享相同的单个名称空间,如果要从默认名称空间更改它,则该名称空间应与Config.WsdlServiceNamespace匹配: http://schemas.servicestack.net/types : http://schemas.servicestack.net/types

通过使用通常在DTO项目的AssemblyInfo.cs文件中定义的[assembly:ContractNamespace]属性,可以轻松完成此操作,这是在ServiceStack.Examples项目中完成此操作的方式:

[assembly: ContractNamespace("http://schemas.servicestack.net/types",
           ClrNamespace = "ServiceStack.Examples.ServiceModel.Operations")]
[assembly: ContractNamespace("http://schemas.servicestack.net/types",
           ClrNamespace = "ServiceStack.Examples.ServiceModel.Types")]

摘自ServiceStack的SOAP支持Wiki页面

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM