简体   繁体   中英

c# Error while deserializing xml to object

I need to test my application that sends xml via post http request. So I send it to myself and try to deserealize. I get "There is an error in XML document (1, 2).". I can create XmlDocument with received xml string, so xml is correct. 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. Now I created a simple server that receives xml via http and tries to deserealize it and copypasted generated classes there. 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:

<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 = ""

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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