如何XML序列化对象列表的数组? [英] How to XML serialize an array of Lists of objects?

查看:95
本文介绍了如何XML序列化对象列表的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将C#对象列表的数组保存在xml文件中。我成功保存了一个对象数组和一个对象列表,但没有保存一个对象列表。

 使用System ; 
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;

命名空间到期
{
公共类Box
{
public int x;
Public Box(int a)
{
x = a;
}
public Box()
{
}
}

public class Program
{
静态无效Main(string [] args)
{
List< Box> [] MainArr = new List< Box> [1];
MainArr [0] = new List< Box>();
Box Box1 = new Box(1);
MainArr [0] .Add(Box1);
System.Xml.Serialization.XmlSerializer writer =新的System.Xml.Serialization.XmlSerializer(MainArr.GetType());
System.IO.StreamWriter fileWrite =新的System.IO.StreamWriter(@ C:\Users\student\Desktop\ArrListBox.xml);
writer.Serialize(fileWrite,MainArr);
fileWrite.Close();
}
}
}

我得到错误:

 无法生成临时类(结果= 1)。 
错误CS1026:)预期
错误CS1002:;预期的
错误CS1525:无效的表达式项’)’
错误CS1002 :;预期的
错误CS1525:无效的表达式术语')'


解决方案

我重现了您的错误消息,并发现它确实是Microsoft的XML序列化程序中的错误。



使用 XmlSerializer(type)时构造函数,.NET自动生成一个名为 AssemblyName.XmlSerializer.dll 的程序集。该程序集包含执行代码序列化和反序列化的实际类。



在空的控制台应用程序中运行此类时,我首先得到一个 NullReferenceException 像其他一些评论一样。当我尝试使用 sgen 自生成程序集时,使用从 List< List< Box>>派生的类时,确实获得了合理的代码。 ; (您只能预编译类,因此不能为 List< Box> [] 预先编译一个序列化程序)。 / p>

使用此类时,我终于收到了错误消息,这可能是您项目中的测试类之一:

 公共类X 
{
public List< Box> [] Boxs {get;组; }
}

使用 sgen

  C:\Program Files(x86)\Microsoft SDKs\Windows\v8.1A\bin \NETFX 4.5.1工具\sgen /t:\"expirement.X /a:ConsoleApplication8.exe / force 

这给了我和你同样的错误。不幸的是,没有快速修复。使用其他类型似乎可以解决问题。在Microsoft Connect上报告此问题。






作为参考,这是序列化的完整代码(使用switch <$ sgen 中的c $ c> / k )。代码似乎破损了,我无法快速修复:

  #if _DYNAMIC_XMLSERIALIZER_COMPILATION 
[assembly:System。 Security.AllowPartiallyTrustedCallers()]
[assembly:System.Security.SecurityTransparent()]
[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
#endif
[assembly:System.Reflection.AssemblyVersionAttribute( 1.0.0.0)]
[assembly:System.Xml.Serialization.XmlSerializerVersionAttribute(ParentAssemblyId = @ c80da358-347f-48cf-88a7-0fda1a15c25b,,版本= @ 4.0.0.0)]
命名空间Microsoft.Xml.Serialization.GeneratedAssembly
{

公共类XmlSerializationWriterX:System.Xml.Serialization.XmlSerializationWriter
{

public void Write4_X(object o)
{
WriteStartDocument();
if(o == null)
{
WriteNullTagLiteral(@ X,@);
的回报;
}
TopLevelElement();
Write3_X(@ X,@,((global :: expirement.X)o),true,false);
}

void Write3_X(字符串n,字符串ns,全局:: expirement.X o,bool isNullable,bool needType)
{
if((object) o == null)
{
if(isNullable)WriteNullTagLiteral(n,ns);
的回报;
}
if(!needType)
{
System.Type t = o.GetType();
if(t == typeof(global :: expirement.X))
{
}
else
{
throw CreateUnknownTypeException(o);
}
}
WriteStartElement(n,ns,o,false,null);
if(needType)WriteXsiType(@ X,@);
{
global :: System.Collections.Generic.List< global :: expirement.Box> [] a =(global :: System.Collections.Generic.List< global :: expirement.Box> [])((global :: System.Collections.Generic.List< global :: expirement.Box> [])o。@ Boxes);
if(a!= null)
{
WriteStartElement(@ Boxes,@,null,false);
for(int ia = 0; ia< a.Length; ia ++)
{
{
global :: System.Collections.Generic.List< global :: expirement。盒子> aa =(global :: System.Collections.Generic.List< global :: expirement.Box>)((global :: System.Collections.Generic.List< global :: expirement.Box>)a [ia]);
if((object)(aa)== null)
{
WriteNullTagLiteral(@ ArrayOfBox,@);
}
else
{
WriteStartElement(@ ArrayOfBox,@,null,false);
for(int iaa = 0; iaa<((System.Collections.ICollection)aa).Count; iaa ++)
{
Write2_Box(@ Box,@,( (global :: expirement.Box)aa [iaa]),是,否)
}
WriteEndElement();
}
}
}
WriteEndElement();
}
}
WriteEndElement(o);
}

void Write2_Box(字符串n,字符串ns,全局:: expirement.Box o,bool isNullable,bool needType)
{
if((object) o == null)
{
if(isNullable)WriteNullTagLiteral(n,ns);
的回报;
}
if(!needType)
{
System.Type t = o.GetType();
if(t == typeof(global :: expirement.Box))
{
}
else
{
throw CreateUnknownTypeException(o);
}
}
WriteStartElement(n,ns,o,false,null);
if(needType)WriteXsiType(@ Box,@);
WriteElementStringRaw(@ x,@,System.Xml.XmlConvert.ToString((global :: System.Int32)((global :: System.Int32)o。@ x)));;
WriteEndElement(o);
}

受保护的重写void InitCallbacks()
{
}
}

公共类XmlSerializationReaderX:System.Xml。 Serialization.XmlSerializationReader
{

公共对象Read4_X()
{
对象o = null;
Reader.MoveToContent();
if(Reader.NodeType == System.Xml.XmlNodeType.Element)
{
if(((object)Reader.LocalName ==(object)id1_X&&(object) Reader.NamespaceURI ==(object)id2_Item))
{
o = Read3_X(true,true);
}
其他
{
throw CreateUnknownNodeException();
}
}
否则
{
UnknownNode(null,@:X);
}
return(object)o;
}

global :: expirement.X Read3_X(bool isNullable,bool checkType){
System.Xml.XmlQualifiedName xsiType = checkType吗? GetXsiType():null;
bool isNull = false;
if(isNullable)isNull = ReadNull();
if(checkType){
if(xsiType == null ||((对象)((System.Xml.XmlQualifiedName)xsiType).Name ==(对象)id1_X&&(对象) ((System.Xml.XmlQualifiedName)xsiType)。命名空间==((object)id2_Item)){
}
否则
抛出CreateUnknownTypeException((System.Xml.XmlQualifiedName)xsiType);
}
如果(isNull)返回null;
global :: expirement.X o;
o =新的global :: expirement.X();
global :: System.Collections.Generic.List< global :: expirement.Box> [] a_0 = null;
int ca_0 = 0;
bool [] paramsRead = new bool [1];
while(Reader.MoveToNextAttribute()){
if(!IsXmlnsAttribute(Reader.Name)){
UnknownNode((object)o);
}
}
Reader.MoveToElement();
if(Reader.IsEmptyElement){
Reader.Skip();
返回o;
}
Reader.ReadStartElement();
Reader.MoveToContent();
int whileIterations0 = 0;
int readerCount0 = ReaderCount;
而(Reader.NodeType!= System.Xml.XmlNodeType.EndElement&& Reader.NodeType!= System.Xml.XmlNodeType.None){
如果(Reader.NodeType == System.Xml .XmlNodeType.Element){
if((((object)Reader.LocalName ==(object)id3_Boxes&&(object)Reader.NamespaceURI ==(object)id2_Item)){
if( !ReadNull()){
global :: System.Collections.Generic.List< global :: expirement.Box> [] a_0_0 = null;
int ca_0_0 = 0;
if(((Reader.IsEmptyElement)){{
Reader.Skip();
}
else {
Reader.ReadStartElement();
Reader.MoveToContent();
int whileIterations1 = 0;
int readerCount1 = ReaderCount;
而(Reader.NodeType!= System.Xml.XmlNodeType.EndElement&& Reader.NodeType!= System.Xml.XmlNodeType.None){
如果(Reader.NodeType == System.Xml .XmlNodeType.Element){
if((((object)Reader.LocalName ==((object)id4_ArrayOfBox&&(object)Reader.NamespaceURI ==(object)id2_Item)){
if( !ReadNull()){
if((object)(a_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box> [])EnsureArrayIndex(a_0_0,ca_0_0,typeof(global: :System.Collections.Generic.List< global :: expirement.Box>));; a_0_0 [ca_0_0 ++])== null)a_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box> [ ])EnsureArrayIndex(a_0_0,ca_0_0,typeof(global :: System.Collections.Generic.List< global :: expirement.Box>))); a_0_0 [ca_0_0 ++] =新的global :: Syst em.Collections.Generic.List< global :: expirement.Box>();
global :: System.Collections.Generic.List< global :: expirement.Box> a_0_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box>)a_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box> [])EnsureArrayIndex(a_0_0, ca_0_0,typeof(global :: System.Collections.Generic.List< global :: expirement.Box>))); a_0_0 [ca_0_0 ++];
if(((Reader.IsEmptyElement)){{
Reader.Skip();
}
else {
Reader.ReadStartElement();
Reader.MoveToContent();
int whileIterations2 = 0;
int readerCount2 = ReaderCount;
而(Reader.NodeType!= System.Xml.XmlNodeType.EndElement&& Reader.NodeType!= System.Xml.XmlNodeType.None){
如果(Reader.NodeType == System.Xml .XmlNodeType.Element){
if((((object)Reader.LocalName ==(object)id5_Box&&(object)Reader.NamespaceURI ==(object)id2_Item)){
if( (对象)(a_0_0_0)== null)Reader.Skip();否则a_0_0_0.Add(Read2_Box(true,true));
}
else {
UnknownNode(null,@:Box);
}
}
else {
UnknownNode(null,@:Box);
}
Reader.MoveToContent();
CheckReaderCount(ref whileIterations2,ref readerCount2);
}
ReadEndElement();
}
}
else {
if((object)(a_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box> []) sureArrayIndex(a_0_0,ca_0_0,typeof(global :: System.Collections.Generic.List< global :: expirement.Box>)); a_0_0 [ca_0_0 ++])== null)a_0_0 =(global :: System.Collections.Generic。 List< global :: expirement.Box> [])EnsureArrayIndex(a_0_0,ca_0_0,typeof(global :: System.Collections.Generic.List< global :: expirement.Box>))); a_0_0 [ca_0_0 ++] =新的global :: System.Collections.Generic.List< global :: expirement.Box>();
global :: System.Collections.Generic.List< global :: expirement.Box> a_0_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box>)a_0_0 =(global :: System.Collections.Generic.List< global :: expirement.Box> [])EnsureArrayIndex(a_0_0, ca_0_0,typeof(global :: System.Collections.Generic.List< global :: expirement.Box>))); a_0_0 [ca_0_0 ++];
}
}
else {
UnknownNode(null,@:ArrayOfBox);
}
}
else {
UnknownNode(null,@:ArrayOfBox);
}
Reader.MoveToContent();
CheckReaderCount(ref whileIterations1,ref readerCount1);
}
ReadEndElement();
}
o。@ Boxes =(global :: System.Collections.Generic.List< global :: expirement.Box> [])ShrinkArray(a_0_0,ca_0_0,typeof(global :: System.Collections .Generic.List< global :: expirement.Box>),false);
}
}
else {
UnknownNode((object)o,@:Boxes);
}
}
else {
UnknownNode((object)o,@:Boxes);
}
Reader.MoveToContent();
CheckReaderCount(ref whileIterations0,ref readerCount0);
}
ReadEndElement();
返回o;
}

global :: expirement.Box Read2_Box(bool isNullable,bool checkType)
{
System.Xml.XmlQualifiedName xsiType = checkType吗? GetXsiType():null;
bool isNull = false;
if(isNullable)isNull = ReadNull();
if(checkType)
{
if(xsiType == null ||((object)((System.Xml.XmlQualifiedName)xsiType).Name ==(object)id5_Box& amp ;(object)((System.Xml.XmlQualifiedName)xsiType).Namespace ==((object)id2_Item))
{
}
否则
throw CreateUnknownTypeException((System.Xml .XmlQualifiedName)xsiType);
}
如果(isNull)返回null;
global :: expirement.Box o;
o =新的global :: expirement.Box();
bool [] paramsRead = new bool [1];
while(Reader.MoveToNextAttribute())
{
if(!IsXmlnsAttribute(Reader.Name))
{
UnknownNode((object)o);
}
}
Reader.MoveToElement();
if(Reader.IsEmptyElement)
{
Reader.Skip();
返回o;
}
Reader.ReadStartElement();
Reader.MoveToContent();
int whileIterations3 = 0;
int readerCount3 = ReaderCount;
而(Reader.NodeType!= System.Xml.XmlNodeType.EndElement&& Reader.NodeType!= System.Xml.XmlNodeType.None)
{
if(Reader.NodeType = = System.Xml.XmlNodeType.Element)
{
if(!paramsRead [0]&&((object)Reader.LocalName ==(object)id6_x&&(object)Reader .NamespaceURI ==(object)id2_Item))
{
{
o。@ x = System.Xml.XmlConvert.ToInt32(Reader.ReadElementString());;
}
paramsRead [0] = true;
}
else
{
UnknownNode((object)o,@:x);
}
}
else
{
UnknownNode((object)o,@:x);
}
Reader.MoveToContent();
CheckReaderCount(ref whileIterations3,ref readerCount3);
}
ReadEndElement();
返回o;
}

受保护的覆盖无效InitCallbacks()
{
}

字符串id5_Box;
字符串id3_Boxes;
字符串id1_X;
字符串id2_Item;
字符串id6_x;
字符串id4_ArrayOfBox;

受保护的覆盖无效InitIDs()
{
id5_Box = Reader.NameTable.Add(@ Box);
id3_Boxes = Reader.NameTable.Add(@ Boxes);
id1_X = Reader.NameTable.Add(@ X);
id2_Item = Reader.NameTable.Add(@);
id6_x = Reader.NameTable.Add(@ x);
id4_ArrayOfBox = Reader.NameTable.Add(@ ArrayOfBox);
}
}

公共抽象类XmlSerializer1:System.Xml.Serialization.XmlSerializer
{
受保护的重写System.Xml.Serialization.XmlSerializationReader CreateReader( )
{
返回新的XmlSerializationReaderX();
}
受保护的重写System.Xml.Serialization.XmlSerializationWriter CreateWriter()
{
返回新的XmlSerializationWriterX();
}
}

公共密封类XSerializer:XmlSerializer1
{

公共重写System.Boolean CanDeserialize(System.Xml.XmlReader xmlReader )
{
返回xmlReader.IsStartElement(@ X,@);
}

受保护的重写无效Serialize(object objectToSerialize,System.Xml.Serialization.XmlSerializationWriter writer)
{
((XmlSerializationWriterX)writer).Write4_X(objectToSerialize) ;
}

受保护的覆盖对象Deserialize(System.Xml.Serialization.XmlSerializationReader reader)
{
return((XmlSerializationReaderX)reader).Read4_X();
}
}

公共类XmlSerializerContract:global :: System.Xml.Serialization.XmlSerializerImplementation
{
公共重写global :: System.Xml。 Serialization.XmlSerializationReader Reader {get {return new XmlSerializationReaderX();
公共重写global :: System.Xml.Serialization.XmlSerializationWriter Writer {get {return new XmlSerializationWriterX();
System.Collections.Hashtable readMethods = null;
公共重写System.Collections.Hashtable ReadMethods
{
get
{
if(readMethods == null)
{
System.Collections .Hashtable _tmp =新的System.Collections.Hashtable();
_tmp [@ expirement.X ::] = @ Read4_X;
if(readMethods == null)readMethods = _tmp;
}
返回readMethods;
}
}
System.Collections.Hashtable writeMethods = null;
公共重写System.Collections.Hashtable WriteMethods
{
get
{
if(writeMethods == null)
{
System.Collections .Hashtable _tmp =新的System.Collections.Hashtable();
_tmp [@ expirement.X ::] = @ Write4_X;
if(writeMethods == null)writeMethods = _tmp;
}
返回writeMethods;
}
}
System.Collections.Hashtable typedSerializers = null;
公共重写System.Collections.Hashtable TypedSerializers
{
如果获得
{
if(typedSerializers == null)
{
System.Collections .Hashtable _tmp =新的System.Collections.Hashtable();
_tmp.Add(@ expirement.X ::,新的XSerializer());
if(typedSerializers == null)typedSerializers = _tmp;
}
return typedSerializers;
}
}
公共重写System.Boolean CanSerialize(System.Type类型)
{
if(type == typeof(global :: expirement.X))返回true;
返回false;
}
公共重写System.Xml.Serialization.XmlSerializer GetSerializer(System.Type类型)
{
if(type == typeof(global :: expirement.X))return新的XSerializer();
返回null;
}
}
}


I am trying to save an array of lists of an object in C# with in a xml file. I succeeded to save a array of an object and a list of objects but not an array of lists of an object.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace expirement
{
    public class Box
    {
        public int x;
        public Box(int a)
        {
            x = a;
        }
        public Box()
        {
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            List<Box>[] MainArr = new List<Box>[1];
            MainArr[0] = new List<Box>();
            Box Box1 = new Box(1);
            MainArr[0].Add(Box1);
            System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(MainArr.GetType());
            System.IO.StreamWriter fileWrite = new System.IO.StreamWriter(@"C:\Users\student\Desktop\ArrListBox.xml");
            writer.Serialize(fileWrite,MainArr);
            fileWrite.Close();
        }
    }
}

i get the error:

Unable to generate a temporary class (result=1).
error CS1026: ) expected
error CS1002: ; expected 
error CS1525: Invalid expression term ')'
error CS1002: ; expected
error CS1525: Invalid expression term ')'

解决方案

I reproduced your error message and found it is indeed a bug in the XML serializer from Microsoft.

When using the XmlSerializer(type) constructor, .NET automatically generates an assembly called AssemblyName.XmlSerializer.dll. This assembly contains the actual classes that do the serialization and deserialization of your code.

When running this in an empty console application, I first got a NullReferenceException like a few others commenting. When I was experimenting with self-generating the assembly using sgen, I did get reasonable code when using a class deriving from List<List<Box>> (you can only pre-compile classes, so you can't pre-compile a serializer for List<Box>[]).

I finally got your error message when using this class, which is possibly one of your test classes in your project:

public class X
{
    public List<Box>[] Boxes { get; set; }
}

Using sgen:

"C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\sgen" /t:"expirement.X" /a:ConsoleApplication8.exe /force

This gives me the same error as you. Unfortunately, there is no quick fix. Using another type seems to do the trick. Report this issue at Microsoft Connect.


As a reference, this is the full code of the serialization (using switch /k in sgen). The code seems so broken, I couldn't quick fix it:

#if _DYNAMIC_XMLSERIALIZER_COMPILATION
[assembly:System.Security.AllowPartiallyTrustedCallers()]
[assembly:System.Security.SecurityTransparent()]
[assembly:System.Security.SecurityRules(System.Security.SecurityRuleSet.Level1)]
#endif
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
[assembly: System.Xml.Serialization.XmlSerializerVersionAttribute(ParentAssemblyId = @"c80da358-347f-48cf-88a7-0fda1a15c25b,", Version = @"4.0.0.0")]
namespace Microsoft.Xml.Serialization.GeneratedAssembly
{

    public class XmlSerializationWriterX : System.Xml.Serialization.XmlSerializationWriter
    {

        public void Write4_X(object o)
        {
            WriteStartDocument();
            if (o == null)
            {
                WriteNullTagLiteral(@"X", @"");
                return;
            }
            TopLevelElement();
            Write3_X(@"X", @"", ((global::expirement.X)o), true, false);
        }

        void Write3_X(string n, string ns, global::expirement.X o, bool isNullable, bool needType)
        {
            if ((object)o == null)
            {
                if (isNullable) WriteNullTagLiteral(n, ns);
                return;
            }
            if (!needType)
            {
                System.Type t = o.GetType();
                if (t == typeof(global::expirement.X))
                {
                }
                else
                {
                    throw CreateUnknownTypeException(o);
                }
            }
            WriteStartElement(n, ns, o, false, null);
            if (needType) WriteXsiType(@"X", @"");
            {
                global::System.Collections.Generic.List<global::expirement.Box>[] a = (global::System.Collections.Generic.List<global::expirement.Box>[])((global::System.Collections.Generic.List<global::expirement.Box>[])o.@Boxes);
                if (a != null)
                {
                    WriteStartElement(@"Boxes", @"", null, false);
                    for (int ia = 0; ia < a.Length; ia++)
                    {
                        {
                            global::System.Collections.Generic.List<global::expirement.Box> aa = (global::System.Collections.Generic.List<global::expirement.Box>)((global::System.Collections.Generic.List<global::expirement.Box>)a[ia]);
                            if ((object)(aa) == null)
                            {
                                WriteNullTagLiteral(@"ArrayOfBox", @"");
                            }
                            else
                            {
                                WriteStartElement(@"ArrayOfBox", @"", null, false);
                                for (int iaa = 0; iaa < ((System.Collections.ICollection)aa).Count; iaa++)
                                {
                                    Write2_Box(@"Box", @"", ((global::expirement.Box)aa[iaa]), true, false);
                                }
                                WriteEndElement();
                            }
                        }
                    }
                    WriteEndElement();
                }
            }
            WriteEndElement(o);
        }

        void Write2_Box(string n, string ns, global::expirement.Box o, bool isNullable, bool needType)
        {
            if ((object)o == null)
            {
                if (isNullable) WriteNullTagLiteral(n, ns);
                return;
            }
            if (!needType)
            {
                System.Type t = o.GetType();
                if (t == typeof(global::expirement.Box))
                {
                }
                else
                {
                    throw CreateUnknownTypeException(o);
                }
            }
            WriteStartElement(n, ns, o, false, null);
            if (needType) WriteXsiType(@"Box", @"");
            WriteElementStringRaw(@"x", @"", System.Xml.XmlConvert.ToString((global::System.Int32)((global::System.Int32)o.@x)));
            WriteEndElement(o);
        }

        protected override void InitCallbacks()
        {
        }
    }

    public class XmlSerializationReaderX : System.Xml.Serialization.XmlSerializationReader
    {

        public object Read4_X()
        {
            object o = null;
            Reader.MoveToContent();
            if (Reader.NodeType == System.Xml.XmlNodeType.Element)
            {
                if (((object)Reader.LocalName == (object)id1_X && (object)Reader.NamespaceURI == (object)id2_Item))
                {
                    o = Read3_X(true, true);
                }
                else
                {
                    throw CreateUnknownNodeException();
                }
            }
            else
            {
                UnknownNode(null, @":X");
            }
            return (object)o;
        }

        global::expirement.X Read3_X(bool isNullable, bool checkType) {
            System.Xml.XmlQualifiedName xsiType = checkType ? GetXsiType() : null;
            bool isNull = false;
            if (isNullable) isNull = ReadNull();
            if (checkType) {
            if (xsiType == null || ((object) ((System.Xml.XmlQualifiedName)xsiType).Name == (object)id1_X && (object) ((System.Xml.XmlQualifiedName)xsiType).Namespace == (object)id2_Item)) {
            }
            else
                throw CreateUnknownTypeException((System.Xml.XmlQualifiedName)xsiType);
            }
            if (isNull) return null;
            global::expirement.X o;
            o = new global::expirement.X();
            global::System.Collections.Generic.List<global::expirement.Box>[] a_0 = null;
            int ca_0 = 0;
            bool[] paramsRead = new bool[1];
            while (Reader.MoveToNextAttribute()) {
                if (!IsXmlnsAttribute(Reader.Name)) {
                    UnknownNode((object)o);
                }
            }
            Reader.MoveToElement();
            if (Reader.IsEmptyElement) {
                Reader.Skip();
                return o;
            }
            Reader.ReadStartElement();
            Reader.MoveToContent();
            int whileIterations0 = 0;
            int readerCount0 = ReaderCount;
            while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && Reader.NodeType != System.Xml.XmlNodeType.None) {
                if (Reader.NodeType == System.Xml.XmlNodeType.Element) {
                    if (((object) Reader.LocalName == (object)id3_Boxes && (object) Reader.NamespaceURI == (object)id2_Item)) {
                        if (!ReadNull()) {
                            global::System.Collections.Generic.List<global::expirement.Box>[] a_0_0 = null;
                            int ca_0_0 = 0;
                            if ((Reader.IsEmptyElement)) {
                                Reader.Skip();
                            }
                            else {
                                Reader.ReadStartElement();
                                Reader.MoveToContent();
                                int whileIterations1 = 0;
                                int readerCount1 = ReaderCount;
                                while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && Reader.NodeType != System.Xml.XmlNodeType.None) {
                                    if (Reader.NodeType == System.Xml.XmlNodeType.Element) {
                                        if (((object) Reader.LocalName == (object)id4_ArrayOfBox && (object) Reader.NamespaceURI == (object)id2_Item)) {
                                            if (!ReadNull()) {
                                                if ((object)(a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++]) == null) a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++] = new global::System.Collections.Generic.List<global::expirement.Box>();
                                                global::System.Collections.Generic.List<global::expirement.Box> a_0_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>)a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++];
                                                if ((Reader.IsEmptyElement)) {
                                                    Reader.Skip();
                                                }
                                                else {
                                                    Reader.ReadStartElement();
                                                    Reader.MoveToContent();
                                                    int whileIterations2 = 0;
                                                    int readerCount2 = ReaderCount;
                                                    while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && Reader.NodeType != System.Xml.XmlNodeType.None) {
                                                        if (Reader.NodeType == System.Xml.XmlNodeType.Element) {
                                                            if (((object) Reader.LocalName == (object)id5_Box && (object) Reader.NamespaceURI == (object)id2_Item)) {
                                                                if ((object)(a_0_0_0) == null) Reader.Skip(); else a_0_0_0.Add(Read2_Box(true, true));
                                                            }
                                                            else {
                                                                UnknownNode(null, @":Box");
                                                            }
                                                        }
                                                        else {
                                                            UnknownNode(null, @":Box");
                                                        }
                                                        Reader.MoveToContent();
                                                        CheckReaderCount(ref whileIterations2, ref readerCount2);
                                                    }
                                                ReadEndElement();
                                                }
                                            }
                                            else {
                                                if ((object)(a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++]) == null) a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++] = new global::System.Collections.Generic.List<global::expirement.Box>();
                                                global::System.Collections.Generic.List<global::expirement.Box> a_0_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>)a_0_0 = (global::System.Collections.Generic.List<global::expirement.Box>[])EnsureArrayIndex(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>));a_0_0[ca_0_0++];
                                            }
                                        }
                                        else {
                                            UnknownNode(null, @":ArrayOfBox");
                                        }
                                    }
                                    else {
                                        UnknownNode(null, @":ArrayOfBox");
                                    }
                                    Reader.MoveToContent();
                                    CheckReaderCount(ref whileIterations1, ref readerCount1);
                                }
                            ReadEndElement();
                            }
                            o.@Boxes = (global::System.Collections.Generic.List<global::expirement.Box>[])ShrinkArray(a_0_0, ca_0_0, typeof(global::System.Collections.Generic.List<global::expirement.Box>), false);
                        }
                    }
                    else {
                        UnknownNode((object)o, @":Boxes");
                    }
                }
                else {
                    UnknownNode((object)o, @":Boxes");
                }
                Reader.MoveToContent();
                CheckReaderCount(ref whileIterations0, ref readerCount0);
            }
            ReadEndElement();
            return o;
        }

        global::expirement.Box Read2_Box(bool isNullable, bool checkType)
        {
            System.Xml.XmlQualifiedName xsiType = checkType ? GetXsiType() : null;
            bool isNull = false;
            if (isNullable) isNull = ReadNull();
            if (checkType)
            {
                if (xsiType == null || ((object)((System.Xml.XmlQualifiedName)xsiType).Name == (object)id5_Box && (object)((System.Xml.XmlQualifiedName)xsiType).Namespace == (object)id2_Item))
                {
                }
                else
                    throw CreateUnknownTypeException((System.Xml.XmlQualifiedName)xsiType);
            }
            if (isNull) return null;
            global::expirement.Box o;
            o = new global::expirement.Box();
            bool[] paramsRead = new bool[1];
            while (Reader.MoveToNextAttribute())
            {
                if (!IsXmlnsAttribute(Reader.Name))
                {
                    UnknownNode((object)o);
                }
            }
            Reader.MoveToElement();
            if (Reader.IsEmptyElement)
            {
                Reader.Skip();
                return o;
            }
            Reader.ReadStartElement();
            Reader.MoveToContent();
            int whileIterations3 = 0;
            int readerCount3 = ReaderCount;
            while (Reader.NodeType != System.Xml.XmlNodeType.EndElement && Reader.NodeType != System.Xml.XmlNodeType.None)
            {
                if (Reader.NodeType == System.Xml.XmlNodeType.Element)
                {
                    if (!paramsRead[0] && ((object)Reader.LocalName == (object)id6_x && (object)Reader.NamespaceURI == (object)id2_Item))
                    {
                        {
                            o.@x = System.Xml.XmlConvert.ToInt32(Reader.ReadElementString());
                        }
                        paramsRead[0] = true;
                    }
                    else
                    {
                        UnknownNode((object)o, @":x");
                    }
                }
                else
                {
                    UnknownNode((object)o, @":x");
                }
                Reader.MoveToContent();
                CheckReaderCount(ref whileIterations3, ref readerCount3);
            }
            ReadEndElement();
            return o;
        }

        protected override void InitCallbacks()
        {
        }

        string id5_Box;
        string id3_Boxes;
        string id1_X;
        string id2_Item;
        string id6_x;
        string id4_ArrayOfBox;

        protected override void InitIDs()
        {
            id5_Box = Reader.NameTable.Add(@"Box");
            id3_Boxes = Reader.NameTable.Add(@"Boxes");
            id1_X = Reader.NameTable.Add(@"X");
            id2_Item = Reader.NameTable.Add(@"");
            id6_x = Reader.NameTable.Add(@"x");
            id4_ArrayOfBox = Reader.NameTable.Add(@"ArrayOfBox");
        }
    }

    public abstract class XmlSerializer1 : System.Xml.Serialization.XmlSerializer
    {
        protected override System.Xml.Serialization.XmlSerializationReader CreateReader()
        {
            return new XmlSerializationReaderX();
        }
        protected override System.Xml.Serialization.XmlSerializationWriter CreateWriter()
        {
            return new XmlSerializationWriterX();
        }
    }

    public sealed class XSerializer : XmlSerializer1
    {

        public override System.Boolean CanDeserialize(System.Xml.XmlReader xmlReader)
        {
            return xmlReader.IsStartElement(@"X", @"");
        }

        protected override void Serialize(object objectToSerialize, System.Xml.Serialization.XmlSerializationWriter writer)
        {
            ((XmlSerializationWriterX)writer).Write4_X(objectToSerialize);
        }

        protected override object Deserialize(System.Xml.Serialization.XmlSerializationReader reader)
        {
            return ((XmlSerializationReaderX)reader).Read4_X();
        }
    }

    public class XmlSerializerContract : global::System.Xml.Serialization.XmlSerializerImplementation
    {
        public override global::System.Xml.Serialization.XmlSerializationReader Reader { get { return new XmlSerializationReaderX(); } }
        public override global::System.Xml.Serialization.XmlSerializationWriter Writer { get { return new XmlSerializationWriterX(); } }
        System.Collections.Hashtable readMethods = null;
        public override System.Collections.Hashtable ReadMethods
        {
            get
            {
                if (readMethods == null)
                {
                    System.Collections.Hashtable _tmp = new System.Collections.Hashtable();
                    _tmp[@"expirement.X::"] = @"Read4_X";
                    if (readMethods == null) readMethods = _tmp;
                }
                return readMethods;
            }
        }
        System.Collections.Hashtable writeMethods = null;
        public override System.Collections.Hashtable WriteMethods
        {
            get
            {
                if (writeMethods == null)
                {
                    System.Collections.Hashtable _tmp = new System.Collections.Hashtable();
                    _tmp[@"expirement.X::"] = @"Write4_X";
                    if (writeMethods == null) writeMethods = _tmp;
                }
                return writeMethods;
            }
        }
        System.Collections.Hashtable typedSerializers = null;
        public override System.Collections.Hashtable TypedSerializers
        {
            get
            {
                if (typedSerializers == null)
                {
                    System.Collections.Hashtable _tmp = new System.Collections.Hashtable();
                    _tmp.Add(@"expirement.X::", new XSerializer());
                    if (typedSerializers == null) typedSerializers = _tmp;
                }
                return typedSerializers;
            }
        }
        public override System.Boolean CanSerialize(System.Type type)
        {
            if (type == typeof(global::expirement.X)) return true;
            return false;
        }
        public override System.Xml.Serialization.XmlSerializer GetSerializer(System.Type type)
        {
            if (type == typeof(global::expirement.X)) return new XSerializer();
            return null;
        }
    }
}

这篇关于如何XML序列化对象列表的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆