先来看xml

<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<Name>小莫</Name>
<Age>20</Age>
<Books>
<Book>
<Title>马列主义</Title>
<ISBN>SOD1323DS</ISBN>
</Book>
</Books>
</Person>
<Person>
<Name>小红</Name>
<Age>20</Age>
<Books>
<Book>
<Title>思想</Title>
<ISBN>SID1323DSD</ISBN>
</Book>
</Books>
</Person>
</root>

这个xml包含多个Person对象,每个Person对象又包含一个Books对象和多个book对象,反序列化XML时关键是看怎么理解xml的结构,理解正确了就很好构造对应的类,理解错了可能就陷入坑里。

首先root是整个文件的根节点,它是由多个Person组成的。

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("root", IsNullable = false)]
public class BaseInfo
{
[System.Xml.Serialization.XmlElementAttribute("Person")]
public List<Person> PersonList { get; set; }
}

再看Person对象,Person是由name和age两个属性和一个Books对象组成,Person可以定义成

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("Person", IsNullable = false)]
public class Person
{
public string Name { get; set; }
public int Age { get; set; } [System.Xml.Serialization.XmlElementAttribute("Books")]
public Books Books { get; set; }
}

这里理解的关键是把下面这小段xml理解成一个包含多个Book的对象,而不是把它理解成book的数组

    <Books>
<Book>
<Title>毛泽思想</Title>
<ISBN>SID1323DSD</ISBN>
</Book>
</Books>

如果把她理解成一个数据就容易把Person定义成

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("Person", IsNullable = false)]
public class Person
{
public string Name { get; set; }
public int Age { get; set; } [System.Xml.Serialization.XmlElementAttribute("Books")]
public List<Book> Books { get; set; }
}

而book的定义如下

   [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("Book", IsNullable = false)]
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
}

序列化生成的xml不包含Book节点

,生成的xml如下
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Person>
<Name>小莫</Name>
<Age>20</Age>
<Books> <Title>马列主义</Title>
<ISBN>SOD1323DS</ISBN> </Books>
</Person>
<Person>
<Name>小红</Name>
<Age>20</Age>
<Books>
<Title>毛泽思想</Title>
<ISBN>SID1323DSD</ISBN> </Books>
</Person>
</root>

之所以出现上面的情况是因为:

public List<Book> Books { get; set; }
和Book同属一个节点层次,在List<Book> Books上显式指定了属性名称Books,那么这个节点就是Books,跟Book上定义指定的[System.Xml.Serialization.XmlRootAttribute("Book", IsNullable = false)]
没有关系,它是用来指示Book是跟节点时的名称。
或者可以这样理解xml是由多个节点组成的,而Book类定义不是一个节点,它只是一个定义,要想xml中出现book,就需要再加一个节点,我们把Person节点加到Books对象中
    [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Books
{
[System.Xml.Serialization.XmlElementAttribute("Book")]
public List<Book> BookList { get; set; }
}

这样就多出了一个节点,再结合刚开始的person定义就能序列化出正确的xml了

完整的代码

 [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("root", IsNullable = false)]
public class BaseInfo
{
[System.Xml.Serialization.XmlElementAttribute("Person")]
public List<Person> PersonList { get; set; }
} [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("Person", IsNullable = false)]
public class Person
{
public string Name { get; set; }
public int Age { get; set; } [System.Xml.Serialization.XmlElementAttribute("Books")]
public Books Books { get; set; }
} [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Books
{
[System.Xml.Serialization.XmlElementAttribute("Book")]
public List<Book> BookList { get; set; }
} [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("Book", IsNullable = false)]
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
}
 static void Main(string[] args)
{ BaseInfo baseInfo = new BaseInfo();
List<Person> personList = new List<Person>();
Person p1 = new Person();
p1.Name = "小莫";
p1.Age = ; List<Book> books = new List<Book>();
Book book = new Book();
book.Title = "马列主义";
book.ISBN = "SOD1323DS";
books.Add(book);
p1.Books = new Books();
p1.Books.BookList = books; Person p2 = new Person();
p2.Name = "小红";
p2.Age = ; List<Book> books2 = new List<Book>();
Book book2 = new Book();
book2.Title = "毛泽思想";
book2.ISBN = "SID1323DSD";
books2.Add(book2);
p2.Books =new Books();
p2.Books.BookList = books2; personList.Add(p1);
personList.Add(p2); baseInfo.PersonList = personList; string postXml2 = SerializeHelper.Serialize(typeof(BaseInfo), baseInfo);
Console.ReadLine();
} }

序列化用到的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml; namespace ConsoleApplication1
{
public class SerializeHelper
{
public static string Serialize(Type type, object o)
{
string result = string.Empty;
try
{
XmlSerializer xs = new XmlSerializer(type);
MemoryStream ms = new MemoryStream();
xs.Serialize(ms, o);
ms.Seek(, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
result = sr.ReadToEnd();
ms.Close();
}
catch (Exception ex)
{
throw new Exception("对象序列化成xml时发生错误:" + ex.Message);
}
return result;
} /// <summary>
/// 序列化XML时不带默认的命名空间xmlns
/// </summary>
/// <param name="type"></param>
/// <param name="o"></param>
/// <returns></returns>
public static string SerializeNoneNamespaces(Type type, object o)
{
string result = string.Empty;
try
{
XmlSerializer xs = new XmlSerializer(type);
MemoryStream ms = new MemoryStream();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");//Add an empty namespace and empty value
xs.Serialize(ms, o, ns);
ms.Seek(, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
result = sr.ReadToEnd();
ms.Close();
}
catch (Exception ex)
{
throw new Exception("对象序列化成xml时发生错误:" + ex.Message);
}
return result;
} /// <summary>
/// 序列化时不生成XML声明和XML命名空间
/// </summary>
/// <param name="type"></param>
/// <param name="o"></param>
/// <returns></returns>
public static string SerializeNoNamespacesNoXmlDeclaration(Type type, object o)
{
string result = string.Empty;
try
{
XmlSerializer xs = new XmlSerializer(type);
MemoryStream ms = new MemoryStream();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;//不编写XML声明
XmlWriter xmlWriter = XmlWriter.Create(ms, settings); ns.Add("", "");//Add an empty namespace and empty value
xs.Serialize(xmlWriter, o, ns);
ms.Seek(, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
result = sr.ReadToEnd();
ms.Close();
}
catch (Exception ex)
{
throw new Exception("对象序列化成xml时发生错误:" + ex.Message);
}
return result;
} public static object Deserialize(Type type, string xml)
{
object root = new object();
try
{
XmlSerializer xs = new XmlSerializer(type);
MemoryStream ms = new MemoryStream();
StreamWriter sw = new StreamWriter(ms);
sw.Write(xml);
sw.Flush();
ms.Seek(, SeekOrigin.Begin);
root = xs.Deserialize(ms);
ms.Close();
}
catch (Exception ex)
{
throw new Exception("xml转换成对象时发生错误:" + ex.Message);
}
return root;
}
}
}

再看一个例子,简单数组类型的序列化
<?xml version="1.0"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<barcodes>
<barcode>AAA</barcode>
<barcode>BBB</barcode>
</barcodes>
</root>

我们经常搞半天不知道要怎么书写上面xml对应的类,总是容易出现上面说过的错误,关键还是看怎么理解barcodes,和上面的例子类似,这里只写类怎么创建

  [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute("root", IsNullable = false)]
public class DownLoadPDFRequest
{ [System.Xml.Serialization.XmlElementAttribute("barcodes")]
public barcoders barcodes { get; set; }
} [System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
//[System.Xml.Serialization.XmlRootAttribute("barcoders", IsNullable = false)]
public class barcoders
{
private List<string> _barcode; [System.Xml.Serialization.XmlElementAttribute("barcode")]
public List<string> BarCode
{
get { return _barcode; }
set { _barcode = value; }
} }
 static void Main(string[] args)
{ DownLoadPDFRequest request = new DownLoadPDFRequest(); List<barcoders> barcodes = new List<barcoders>(); List<string> bars=new List<string>();
bars.Add("AAA");
bars.Add("BBB");
request.barcodes = new barcoders();;
request.barcodes.BarCode = bars; string postXml = SerializeHelper.Serialize(typeof(DownLoadPDFRequest), request);
}


C# xml数组的序列和反序列化的更多相关文章

  1. .NET Core 对象到字节数组的序列化和反序列化

    .NET Core中利用MemoryStream和BinaryFormatter可以实现对象到字节数组的序列化和反序列化: 定义ObjectSerializer类,实现对象到字节数组的序列化和反序列化 ...

  2. 理解PHP数组的序列化和反序列化

    当我们想要将数组值存储到数据库时,就可以对数组进行序列化操作,然后将序列化后的值存储到数据库中.其实PHP序列化数组就是将复杂的数组数据类型转换为字符串,方便数组存库操作.对PHP数组进行序列化和反序 ...

  3. .NET(C#)使用Serialize、Deserialize序列和反序列化XML文档

    本文给大家分享一下C#操作(读取.写入)XML文档的实用方法,即用.NET本身提供的Deserialize和Serialize进行反序列化和序列化XML文档.这种方法主要是对比较规范的XML文档进行操 ...

  4. 对象序列和反序列化Xml

    1. XmlArray和XmlArrayItem XmlArray和XmlArrayItem是不同的,XmlArray是指这个数组叫什么,XmlArrayItem 值数组的每个元素叫什么. <X ...

  5. xml对象的序列化和反序列化

    对象序列化: /// <summary>        /// 将一个对象序列化为XML字符串        /// </summary>        /// <par ...

  6. json数组的序列化和反序列化json数组的序列化和反序列化

    如题,我就不多说了,自己看代码的,很好理解 using System; using System.Collections.Generic; using System.Web; using System ...

  7. XML数组和对象,反之亦然

    惊人的互相转换,还是因为麻烦.程序很反感麻烦猿 1 阵转xml <?php /* 一维数组转xml 思路: 循环数组每一个单元,添加到xml文档节点中去 */ /* $arr = array( ...

  8. POJ--3321 Apple Tree(树状数组+dfs(序列))

    Apple Tree Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22613 Accepted: 6875 Descripti ...

  9. php 数组转xml 数组转json xml转数组 json转数组

    array->xml <?php function array2xml($array, $tag) { function ia2xml($array) { $xml="" ...

随机推荐

  1. Object type TYPE failed to create with error

    ORA-39083: Object type TYPE failed to create with error: ORA-02304: invalid object identifier litera ...

  2. Spring事务的5种隔离级别和7种传播性

    隔离级别 isolation,5 种: ISOLATION_DEFAULT,ISOLATION_READ_UNCOMMITTED,ISOLATION_READ_COMMITTED,ISOLATION_ ...

  3. MVC教程四:Controller向View传值的几种方式

    一.通过ViewData传值 MVC从开始版本就一直支持使用ViewData将Controller里面的数据传递到View.ViewData定义如下: 从上面的截图中可以看出,ViewData里面存的 ...

  4. 微信小程序使用npm安装包

    小程序现在支持直接通过npm安装包了,点击这里了解更多. 记录一下我自己的安装步骤及安装过程中遇到的一些问题.希望能够帮助到正在阅读此篇文章的你~ 我就直接通过在项目根目录安装miniprogram- ...

  5. ScheduledThreadPoolExecutor实现原理

    ScheduledThreadPoolExecutor实现原理 博客分类: concurrency java   自jdk1.5开始,Java开始提供ScheduledThreadPoolExecut ...

  6. spingboot中的美女banner.txt

    .::::. .::::::::. ::::::::::: ..:::::::::::' '::::::::::::' .:::::::::: '::::::::::::::.. ..:::::::: ...

  7. python定义函数时,形参前加*和**的意义

    转发:https://blog.csdn.net/qq_34806812/article/details/82017839 1.加*表示接受一个tuple类型(元组),如: 2.加**表示接受一个di ...

  8. javascript实现限定高度下文字随不同设备自适应改变字体大小至字数完全展示

    function fontAutoMoreLine() { let textBox = document.getElementById("iconTxt"); let maxHei ...

  9. Linux下常见命令

    =============挂载和登陆命令======================================== Mount:挂载命令. 比方挂载光驱mount /dev/cdrom /mnt ...

  10. MySQL 全文检索 ngram插件

    InnoDB全文索引:N-gram Parser[转] MySql5.7 建立全文索引 InnoDB默认的全文索引parser非常合适于Latin,因为Latin是通过空格来分词的.但对于像中文,日文 ...