繁体   English   中英

XML文档(1,1)中存在错误。 反序列化时

[英]There is an error in XML document (1, 1). while deserializing

我正在使用Silverlight进行序列化和反序列化。

我通过从类和在序列化时获得的xml(我尝试反序列化的xml)中获取对象,成功完成了序列化。

我这样做的代码是:

namespace SliderLastTry
{
    public class ControlClass
    {
        public void Main()
        {

            Parameters pc = new Parameters()
              {
                  Parameter = { new Parameter { Name = "Name1", Label = "Label1", Unit = "Uint1", Component = { new Component { Type = "Type1", Attributes = { new Attributes { Type = "Combo", Displayed = "42", Selected = "02", Items = { "10", "11", "12", "13", "14" } } } } } }, { new Parameter { Name = "Name2", Label = "Label2", Unit = "Uint2", Component = { new Component { Type = "Type2", Attributes = { new Attributes { Type = "Slider", Displayed = "52", Selected = "05", Items = { "20", "21", "22", "23", "24" } } } } } } } }
               ,
                  Separator = { new Separator { Separators = "AutoSkew1" } }
              };
            String xml = pc.ToXml(); //This function serializes and returns xml.
            XmlSerializer deserializer = new XmlSerializer(typeof(Parameters));
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            Parameters parameter = (Parameters)deserializer.Deserialize(reader);
        }
    }

这段代码中的这一行给了我异常:

Parameters parameter = (Parameters)deserializer.Deserialize(reader);

这是用于序列化的代码(我的意思是定义MyXML的类):

namespace SliderLastTry
{
    public static class Xml
    {
        public static string ToXml(this object objectToSerialize)
        {
             var memory = new MemoryStream();
             var serial = new XmlSerializer(objectToSerialize.GetType());
             serial.Serialize(memory, objectToSerialize);  
            var utf8 = new UTF8Encoding();
            return utf8.GetString(memory.GetBuffer(), 0, (int)memory.Length);  
        }
    }
}

得到的警告是:

There is an error in XML document (1, 1).

请详细查看异常:

内部异常:

 {System.Xml.XmlException: Invalid data at root level. Line 1, position 1. 
        to System.Xml.XmlTextReaderImpl.Throw (Exception e) 
        to System.Xml.XmlTextReaderImpl.Throw (String res, String arg) 
        System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace to () 
        System.Xml.XmlTextReaderImpl.ParseDocumentContent to () 
        System.Xml.XmlTextReaderImpl.Read to () 
        System.Xml.XmlReader.MoveToContent to () 
        Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderParameters.Read7_parameters to ()}

在堆栈跟踪:

at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle, Object events)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader, String encodingStyle)
at System.Xml.Serialization.XmlSerializer.Deserialize (XmlReader xmlReader)
SliderLastTry.ControlClass.Main to ()
ctor to SliderLastTry.MainPage .. ()
at SliderLastTry.App.Application_Startup (Object sender, e StartupEventArgs)
at MS.Internal.CoreInvokeHandler.InvokeEventHandler (UInt32 indexType, handlerDelegate Delegate, Object sender, Object args)
at MS.Internal.JoltHelper.FireEvent (unmanagedObj IntPtr, IntPtr unmanagedObjArgs, argsTypeIndex Int32, Int32 actualArgsTypeIndex, String eventName, UInt32 flags)

毕竟我为此找到了解决方案。 重新设置此:

var memory = new MemoryStream();
             var serial = new XmlSerializer(objectToSerialize.GetType());
             serial.Serialize(memory, objectToSerialize);  
            var utf8 = new UTF8Encoding();
            return utf8.GetString(memory.GetBuffer(), 0, (int)memory.Length);

var writer = new StringWriter();
            var serializer = new XmlSerializer((objectToSerialize.GetType()));
            serializer.Serialize(writer, objectToSerialize);
            string xml = writer.ToString();
            return xml;

之所以起作用,是因为StringReaderutf8兼容。

暂无
暂无

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

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