我使用 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);
Thsi是序列化的代码(我的意思是定义了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)
答案 0 :(得分:1)
毕竟我找到了解决方案。 取而代之:
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;
工作,因为StringReader
与utf8
兼容。