tags:

views:

201

answers:

1

when by posting the xml message from client to WCF service, when by getting save the xml file we lost the root element(for eg: tag) in the xml content

Server Code :

[ServiceContract(Namespace = "http://www.mydomain.com/testing")]
public interface Imyservice
{
    [OperationContract]
    [WebInvoke(UriTemplate = "data", Method = "POST" ,BodyStyle = WebMessageBodyStyle.Bare,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    Stream postdata(XmlElement input);
}

-------------------------------------------------------
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(Namespace = "http://www.mydomain.com/testing")]
[XmlRootAttribute(ElementName = "Message", IsNullable = false)]
public class myservice : Imyservice
{
   public Stream postdata([XmlAnyElement]**XmlElement** input)
   {
        string str = input.InnerXml.ToString();
        String filepath = "D:\\WebFiles\\temp\\a.xml";
        FileStream fs = File.Create(filepath);
        byte[] XMLBytes = new System.Text.UTF8Encoding(true).GetBytes(str);
        fs.Write(XMLBytes, 0, XMLBytes.Length);
        fs.Close();

        -----

        Encoding encoding = Encoding.GetEncoding("ISO-8859-1");
        WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
        byte[] returnBytes = encoding.GetBytes(pstrstring);
        return new MemoryStream(returnBytes);
    }
}

Binding : webHttpBinding

===========================================================

Client HTTP post the data

input ::

<message>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
</message>


" Only one top level element is allowed in an XML document. Error processing resource"

ROOT tag is missing in the a.xml in server

   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies, 
      an evil sorceress, and her own childhood to become queen 
      of the world.</description>
   </book>
A: 

Why do you want to post manually constructed XML as such to a WCF service?

Typically, what you want to do is create a DataContract - a set of objects that get passed around. The benefit being: you don't have to deal with handling all the XML serialization and deserialization.

What happens here is that the message that goes from client to server is already an XML message - into which you stick another XML fragment. This is just calling for trouble.....

What's the original data you have? Do you have a "Book" type? If so - why not using:

[ServiceContract(Namespace = "http://www.mydomain.com/testing")]
public interface IMyBookService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "data", Method = "POST" ,BodyStyle = WebMessageBodyStyle.Bare,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    Stream postdata(Book myBook);
}

or

[ServiceContract(Namespace = "http://www.mydomain.com/testing")]
public interface IMyBookService
{
    [OperationContract]
    [WebInvoke(UriTemplate = "data", Method = "POST" ,BodyStyle = WebMessageBodyStyle.Bare,RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml)]
    Stream postdata(List<Book> myBookList);
}

Wouldn't that be a lot easier? WCF is designed to take care of all these things - why do everything manually by hand, if you can let WCF handle all the nitty-gritty details??

Marc

marc_s