如何在数据契约中定义字符串数组?

时间:2014-01-11 04:24:46

标签: datacontractserializer datacontract wcf-serialization

我在datacontract中有一个字符串数组,如下所示

[DataMember(Name = "photos", IsRequired = true, Order = 3)] 
public string[] photos { get; set; }

在WCF REST服务调用中,我传递的是此字符串数组

的xml输入
<photos>
 <string>http://localhost:13169/MainPortal/ContentLibrary/21/58/1227132014-bmw-x1_100426158_m.jpg</string>
 <string>http://localhost:13169/MainPortal/ContentLibrary/21/58/122713bmw2.jpg</string>
 <string>http://localhost:13169/MainPortal/ContentLibrary/21/58/122713bmw3.jpg</string>
 <string>http://localhost:13169/MainPortal/ContentLibrary/21/58/122713BMW_Hamann_Laguna_Seca.jpg</string><string>http://localhost:13169/MainPortal/ContentLibrary/21/58/1227132014-BMW-X1-SUV-sDrive-28i-4dr-4x2-Sports-Activity-Vehicle-Exterior-2.png</string>
</photos>

我的客户端代码能够通过在HttpWebRequest中传递xml来进行WebService调用而没有任何问题,从服务中我可以看到除了这个字符串数组之外的所有其他数据。我在DataContract中遗漏了一些序列化数组的东西。请帮忙

尝试使用CollectionDataContract,但仍然没有运气

[CollectionDataContract(Name = "photos")]
public class Photos : Collection<string>

还添加了KnownTypes,但序号类型

不需要
[KnownType(typeof(string[]))]
public class PublishPhotos
{

这是完整的数据合同类

[DataContract(Namespace = "http://myurl.com/Publisher")]
[KnownType(typeof(string[]))]
public class PublishPhotos
{
    [DataMember(Name = "publicationId", IsRequired = true, Order = 0)]
    public int publicationId { get; set; }

    [DataMember(Name = "issueId", IsRequired = true, Order = 1)]
    public string issueId { get; set; }

    [DataMember(Name = "contentId", IsRequired = true, Order = 2)]
    public string contentId { get; set; }

    [DataMember(Name = "photos", IsRequired = true, Order = 3)]
    public Photos photos { get; set; }

}

2 个答案:

答案 0 :(得分:1)

最后我弄清楚HttpWebRequest中的XML出了什么问题,问题在于序列化字符串数组。我可以通过访问/PublisherSvc.svc/help获取字符串的最简单方法,它需要名称空间http://schemas.microsoft.com/2003/10/Serialization/Arrays,如下所示

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">http://localhost:8081/photos/1/1.jpg</string>

希望这可以帮助面临类似问题的人

答案 1 :(得分:0)

我已经找到了这个问题的答案。感谢@HarlanB

我已将数据合同从DataContract Serialization更改为XmlSerialization

[SerializableAttribute()]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "http://myUrl.com/Publisher", IsNullable = false)]
public class PublishPhotos
{
    //[DataMember(Name = "publicationId", IsRequired = true, Order = 0)]
    [XmlElementAttribute(ElementName="publicationId", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
    public int publicationId { get; set; }

    //[DataMember(Name = "issueId", IsRequired = true, Order = 1)]
    [XmlElementAttribute(ElementName = "issueId", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
    public string issueId { get; set; }

    //[DataMember(Name = "contentId", IsRequired = true, Order = 2)]
    [XmlElementAttribute(ElementName = "contentId", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 2)]
    public string contentId { get; set; }

    //[DataMember(Name = "photos", IsRequired = true, Order = 3)]
    [XmlElementAttribute(ElementName = "photos", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 3)]
    public string[] photos { get; set; }

}

在客户端代码中,我使用XmlSerializer写入HttpWebRequest,如下所示

            pubPhotos.publicationId = Convert.ToInt32(publication.Value);
            pubPhotos.issueId = secName;
            pubPhotos.contentId = selectedMediaItem;

            HtmlDocument divDoc = new HtmlDocument();
            divDoc.LoadHtml(widgetScript);
            HtmlNode divNode = divDoc.DocumentNode.FirstChild;
            HtmlNodeCollection imgs = divNode.SelectNodes("//img");
            Collection<string> photos = new Collection<string>();
            foreach (HtmlNode img in imgs)
            {
                photos.Add(img.Attributes["src"].Value);
            }
            pubPhotos.photos = photos.ToArray();
        HttpWebRequest req = null;
            const string url = "http://localhost:40009/PublisherSvc.svc/PublishPhotos";
            req = (HttpWebRequest)WebRequest.Create(url);
            req.Method = "POST";
            req.ContentType = "application/xml; charset=utf-8";
            req.KeepAlive = false;
            req.Timeout = 30000;
            req.Headers.Add("SOAPAction", url);

            XmlSerializer serilizer = new XmlSerializer(typeof(PublishPhotos));
            var sw = new StreamWriter(req.GetRequestStream());
            serilizer.Serialize(sw, pubPhotos);
            sw.Close();

我希望这可以帮助那些有类似问题的其他人。