如何在xml反序列化中解决无效的Operation Exception?

时间:2014-04-14 14:31:59

标签: c# xml serialization

我想在下面的xml中进行反序列化但它产生错误:XML文档中存在错误(2,2)。内部例外:{“没想到。”}

如何反序列化我的xml代码?我如何解决以下错误?

我的C#代码:

  class Program
{
    static void Main(string[] args)
    {
        GetData();
    }

    static void GetData()
    {
        string actionUrl = "http://test/vrm_search?q=56071794482024887&vrm_type=corvus";

        HttpWebRequest request = WebRequest.Create(actionUrl) as HttpWebRequest;

        request.Method = "GET";
        request.ContentType = "application/xml";
        request.Accept = "application/xml";


        string responseData = string.Empty;

        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            using (StreamReader reader = new StreamReader(response.GetResponseStream()))
            {
                responseData = reader.ReadToEnd();
                Console.Write(responseData);
                reader.Close();
            }

            response.Close();
        }

        Response res = XML.Deserialize<Response>(responseData);

        Console.Read();
    }

}


public static class XML
{

    public static T Deserialize<T>(string xml)
    {
        if (string.IsNullOrEmpty(xml))
        {
            return default(T);
        }

        XmlSerializer serializer = new XmlSerializer(typeof(T));
        XmlReaderSettings settings = new XmlReaderSettings();

        using (StringReader textReader = new StringReader(xml))
        {
            using (XmlReader xmlReader = XmlReader.Create(textReader, settings))
            {
                return (T)serializer.Deserialize(xmlReader);
            }
        }
    }

}

ERROR:

enter image description here

1 个答案:

答案 0 :(得分:2)

它正在寻找一个“x”元素,而不是xml中的“x”元素。 (注意外壳)