1

I have a string array in datacontract as below

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

In WCF REST Service call I am passing below xml input for this string array

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

My client code able to make WebService call with passing xml in HttpWebRequest without any issues, from the service I could see all other data coming in except this string array. Am I missing something in DataContract to serialize arrays. Please help

Tried with CollectionDataContract as below still no luck

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

Also added KnownTypes which is however not required for ordinal types

[KnownType(typeof(string[]))]
public class PublishPhotos
{

Here is complete data contract class

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

}
Naga
  • 2,368
  • 3
  • 23
  • 33

2 Answers2

2

Finally I figured out what went wrong with XML in HttpWebRequest, the issue is with serializing string array. The easiest way I could have figured out through visiting /PublisherSvc.svc/help for string it requires namespace http://schemas.microsoft.com/2003/10/Serialization/Arrays as below

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

Hope this helps someone facing similar issue

Naga
  • 2,368
  • 3
  • 23
  • 33
0

I have figured out the answer to this problem. Thanks to @HarlanB

I have changed data contract from DataContract Serialization to 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; }

}

In client code I am using XmlSerializer to write to HttpWebRequest as below

            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();

I hope this helps some other people out there that are having similar problems.

Naga
  • 2,368
  • 3
  • 23
  • 33
  • The above client solution is for WCF not for WCF REST. For REST client should be able to write just the XML not the object. Looks like there is something wrong with XML schema need to figure that out. – Naga Jan 12 '14 at 05:28