locked
XmlElement xml data RRS feed

  • Question

  • 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: <Message> 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);

        }

    }

    Bidning : 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 <Message> 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>


    please advise !!


    Thanks, Kanna
    Wednesday, September 16, 2009 11:52 AM

Answers



  • Changed the server code   like   string q = input.OuterXml.ToString();   instead of

    string str = input.InnerXml.ToString();

    Now working fine


    Thanks, Kanna
    • Marked as answer by Senthil Veera Thursday, September 17, 2009 4:36 AM
    Thursday, September 17, 2009 4:36 AM