开发者

c# object to xml, rest service

I have been having problems with the 400 Bad Response, and have got it down to the XML sting that is getting passed. The string that works is

<Data xmlns=\"http://www.eysnap.com/mPlayer\">
<Name>Chris</Name>
<Age>29</Age>
<Period>123</Period>
<msg>Why</msg>
</Data>

However when I try and seralize it I开发者_StackOverflow get:

<?xml version="1.0" encoding="utf-16"?>
<Data xmlns:Namespace="http://www.eysnap.com/mPlayer">
<Name>Chris</Name>
<Age>29</Age>
<Period>123</Period>
<msg>Why</msg>
</Data>

I know that is the correct formatting, but I either need to set the xml output to a bare view or I need to configure my service to accept the correct view.

Client Post Code:

        StringBuilder s = new StringBuilder();
        StringWriter writter = new StringWriter(s);

        XmlWriterSettings settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration=true;

        using(XmlWriter writer = XmlWriter.Create(writter,settings)){
        XmlSerializer x = new XmlSerializer(d.GetType());
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("Namespace","http://www.eysnap.com/mPlayer");
        x.Serialize(writter,d,ns);
        }

        string invalid = s.ToString();
        string valid ="<Data xmlns=\"http://www.eysnap.com/mPlayer\"><Name>Chris</Name><Age>29</Age><Period>12 Years</Period><msg>Fucking hell</msg></Data>";
            req.ContentLength = valid.Length;
            var sw = new StreamWriter(req.GetRequestStream());
            sw.Write(valid);
            sw.Close();

            res = (HttpWebResponse) req.GetResponse();
            Stream responseStream = res.GetResponseStream();
            var streamReader = new StreamReader(responseStream);

            //Read the response into an xml document
            var soapResonseXmlDocument = new XmlDocument();
            soapResonseXmlDocument.LoadXml(streamReader.ReadToEnd());

            //return only the xml representing the response details (inner request)
            TextBox1.Text = soapResonseXmlDocument.InnerXml;
            //Response.Write(soapResonseXMLDocument.InnerXml);

Rest Service Interface:

    [OperationContract]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Xml,
        RequestFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped,
        UriTemplate = "auth")]
    ResponseData Auth(Data rData);

Thanks

Chris


What happens if you do this instead:

XmlSerializer x = new XmlSerializer(d.GetType(), "http://www.eysnap.com/mPlayer");
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(String.Empty, "http://www.eysnap.com/mPlayer");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜