无法序列化来自oauth RESTful webservice的响应 - XML Doc(1,2)中的错误

时间:2014-02-11 13:31:52

标签: c# web-services xml-parsing xml-serialization xmlserializer

我一直在寻找这个问题几个小时的答案,我读的越多,我似乎就越困惑!基本上问题是,我试图从我的Web服务序列化响应,但不能将xml转换回对象

首先,我的网络服务有这个类对象

[Serializable()]
    [XmlRootAttribute("WcfRESTful")]
    public class testcc
    {

        [System.Xml.Serialization.XmlElement("field1")]
        public string f1 { get; set; }
        [System.Xml.Serialization.XmlElement("field2")]
        public string f2 { get; set; }
        [System.Xml.Serialization.XmlElement("field3")]
        public string f3 { get; set; }


        public testcc() { }

        public testcc(string _f1, string _f2, string _f3)
        {
            f1 = _f1;
            f2 = _f2;
            f3 = _f3;
        }
}

要在Web服务端序列化数据,请执行以下操作

public string GetTest_With_OAuth(string username)
        {
            if (Authenticate(WebOperationContext.Current.IncomingRequest))
            {
                try
                {

                    //Serialize emp in the xmlTextWriter
                    List<testcc> emplist = new List<testcc>();
                    testcc emp = new testcc("Hello","Some", "Person");    
                    string XmlizedString = StringSerialize(emp);
                    return XmlizedString;
                }
                catch (Exception ex)
                {
                    errHandler err = new errHandler();
                    err.ErrorMessage = ex.Message.ToString();
                    throw;
                }
            }
            else
            {
                //Invalid User
            }
        }

private string StringSerialize(testcc data)
        {
            TextWriter w = WriterSerialize(data);
            string xml = w.ToString();
            w.Close();
            return xml;
        }

        private TextWriter WriterSerialize(testcc data)
        {
            TextWriter w = new StringWriter();
            this.s = new XmlSerializer(typeof(WcfRESTful.testcc));
            s.Serialize(w, data);
            w.Flush();
            return w;
        }

所以基本上它将以下数据作为字符串返回给我,然后我尝试序列化

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/"><?xml version="1.0" encoding="utf-16"?> <WcfRESTful xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <field1>Hello</field1> <field2>Some</field2> <field3>Person</field3> </WcfRESTful></string> 

这是我用来反序列化的代码

public testcc DeserializeString(string xml)
        {
            TextReader reader = new StringReader(xml);
            return Deserialize(reader);
        }

public testcc Deserialize(TextReader reader)
        {
            testcc o = (testcc)s.Deserialize(reader);
            reader.Close();
            return o;
        }

每次虽然它在testcc o = (testcc)s.Deserialize(reader);上失败但是XML文档中存在错误(1,2)并且我似乎无法弄明白但它基本上是我需要为我的webservice id做的最后一件事= f任何人都有任何意见我会很感激。我不确定它是否与标签有关,甚至不知道如何删除它们:/

修改

在客户端,我正在解析webresponse,如下所示

serilizer serializer = new serilizer();
                try
                {
                    string xml = string.Empty;
                    using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                    {
                        xml = reader.ReadToEnd();
                        reader.Close();
                    }
                    testcc item = serializer.DeserializeString(xml);
                }
                catch(Exception ex)
                {
                    Response.Write(ex.Message);
                }

我在testcc item = serializer.DeserializeString(xml);有一个断点,我正在查看string xml变量

0 个答案:

没有答案