简体   繁体   English

将反序列化xml到对象时出现c#错误

[英]c# Error while deserializing xml to object

I need to test my application that sends xml via post http request. 我需要测试通过post http请求发送xml的应用程序。 So I send it to myself and try to deserealize. 因此,我将其发送给自己,并尝试去实现。 I get "There is an error in XML document (1, 2).". 我收到“ XML文档(1、2)中有错误。”。 I can create XmlDocument with received xml string, so xml is correct. 我可以使用收到的xml字符串创建XmlDocument,因此xml是正确的。 I think i get the exception because I created schema from different source, ie I copypasted my classes for xml into another application, created schema using that application, then I created classes from this schema. 我认为我得到了例外,因为我从不同的源创建了架构,即我将xml的类复制粘贴到另一个应用程序中,使用该应用程序创建了架构,然后从该架构中创建了类。 Now I created a simple server that receives xml via http and tries to deserealize it and copypasted generated classes there. 现在,我创建了一个简单的服务器,该服务器通过http接收xml,并尝试将其退化并复制粘贴到该处生成的类。 Here's the code: 这是代码:

static void Main(string[] args)
        {
            HttpListener listener = new HttpListener();
            listener.Prefixes.Add(@"http://127.0.0.1:123/ololo/");
            listener.Start();
            var context = listener.GetContext();
            var xmlstring = string.Empty;

            using (var sr = new StreamReader(context.Request.InputStream))
            {
                xmlstring = sr.ReadToEnd();
            }
            XmlDocument xmlka = new XmlDocument();
            xmlka.LoadXml(xmlstring);
            XmlSerializer serializer = new XmlSerializer(typeof(XmlData));
            MemoryStream memStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlstring));
            try
            {
                XmlData resultingMessage = (XmlData)serializer.Deserialize(memStream);
            }
            catch(Exception ex)
            {

            }
        }

Stack trace: 堆栈跟踪:

   at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
   at System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
   at ConsoleApplication1.Program.Main(String[] args) in c:\\users\\jamil\\documents\\visual studio 2015\\Projects\\ConsoleApplication1\\ConsoleApplication1\\Program.cs:line 45

InnerException message: InnerException消息:

<XmlData xmlns='http://schemas.datacontract.org/2004/07/Common.Util'> was not expected.

All indexes in generated classes look like this: 生成的类中的所有索引如下所示:

   // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------

    // 
    // This source code was auto-generated by xsd, Version=4.6.1055.0.
    // 


    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]

What confuses me about this is Namespace = "" 令我感到困惑的是Namespace =“”

您可以尝试在根类XmlData添加名称空间:

[XmlRoot(ElementName = "XmlData", Namespace = "http://schemas.datacontract.org/2004/07/Common.Util")]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM