1

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

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

}
4

2 回答 2

2

最后我弄清楚了 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>

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

于 2014-01-13T17:31:55.360 回答
0

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

我已将数据合同从 DataContract 序列化更改为 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();

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

于 2014-01-11T15:17:55.500 回答