溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

根據(jù)xsd序列化與反序列化xml

發(fā)布時間:2020-06-23 02:56:06 來源:網(wǎng)絡(luò) 閱讀:940 作者:zj6882917 欄目:編程語言
1,建立xsd
step1:建立“類型”Version、Updatetime,F(xiàn)iles,F(xiàn)ile
step2:建立Files與File的多對一關(guān)系,添加Files中的file引用(是File類型的),修改file的屬性maxOccurs為unbounded,minOccurs為1
如圖:
根據(jù)xsd序列化與反序列化xml
step3:建立頂級元素“類型”Update,添加version,updatetime,files的引用,如圖
根據(jù)xsd序列化與反序列化xml
step4:添加頂級元素update(類型為Update),如圖
根據(jù)xsd序列化與反序列化xml

xsd代碼如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="UpdateFile" targetNamespace="http://tempuri.org/UpdateFile.xsd" elementFormDefault="qualified" xmlns="http://tempuri.org/UpdateFile.xsd" xmlns:mstns="http://tempuri.org/UpdateFile.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType name="Update">
        <xs:sequence>
            <xs:element name="version" type="Version" />
            <xs:element name="updatetime" type="Updatetime" />
            <xs:element name="files" type="Files" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Version">
        <xs:sequence>
            <xs:element name="value" type="xs:string" />
            <xs:element name="type" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Updatetime">
        <xs:sequence>
            <xs:element name="value" type="xs:dateTime" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Files">
        <xs:sequence>
            <xs:element name="file" type="File" maxOccurs="unbounded" minOccurs="1" />
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="File">
        <xs:sequence>
            <xs:element name="url" type="xs:string" />
        </xs:sequence>
    </xs:complexType>
    <xs:element name="update" type="Update">
    </xs:element>
</xs:schema>

2,生成實(shí)體類
用vs命令行在項(xiàng)目文件夾下輸入以下命令
xsd.exe 要生成實(shí)體類的.xsd /c /namespace:要生成的實(shí)體類的命名空間


3,兩個靜態(tài)工具類
根據(jù)xsd序列化與反序列化xmlclass Common
根據(jù)xsd序列化與反序列化xml        {
根據(jù)xsd序列化與反序列化xml                /// <summary>
根據(jù)xsd序列化與反序列化xml                /// 將XML文件寫入指定的對象
根據(jù)xsd序列化與反序列化xml                /// </summary>
根據(jù)xsd序列化與反序列化xml                /// <param name="xmlFile">xml絕對路徑</param>
根據(jù)xsd序列化與反序列化xml                /// <param name="type">序列的類型,要與XML對應(yīng)的類</param>
根據(jù)xsd序列化與反序列化xml                /// <returns>將對象返回,當(dāng)文件操作失敗則返回Null值</returns>
根據(jù)xsd序列化與反序列化xml                public static object DeserializeXmlToObject(string xmlFile, Type type)
根據(jù)xsd序列化與反序列化xml                {
根據(jù)xsd序列化與反序列化xml                        XmlSerializer mySerializer = new XmlSerializer(type);
根據(jù)xsd序列化與反序列化xml                        using (FileStream stream = new FileStream(xmlFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
根據(jù)xsd序列化與反序列化xml                        {
根據(jù)xsd序列化與反序列化xml                                return mySerializer.Deserialize(stream);
根據(jù)xsd序列化與反序列化xml                        }
根據(jù)xsd序列化與反序列化xml                }
根據(jù)xsd序列化與反序列化xml
                /// <summary>
根據(jù)xsd序列化與反序列化xml                ///    將對象寫入到XML中
根據(jù)xsd序列化與反序列化xml                /// </summary>
根據(jù)xsd序列化與反序列化xml                /// <param name="obj">數(shù)據(jù)源對象</param>
根據(jù)xsd序列化與反序列化xml                /// <param name="xmlFile">目標(biāo)路徑</param>
根據(jù)xsd序列化與反序列化xml                /// <param name="type">轉(zhuǎn)換類型</param>
根據(jù)xsd序列化與反序列化xml                public static void SerializeObjectToXml(object obj, String xmlFile, Type type)
根據(jù)xsd序列化與反序列化xml                {
根據(jù)xsd序列化與反序列化xml                        XmlSerializer mySerializer = new XmlSerializer(type);
根據(jù)xsd序列化與反序列化xml                        using (FileStream stream = new FileStream(xmlFile, FileMode.Create, FileAccess.Write, FileShare.Read))
根據(jù)xsd序列化與反序列化xml                        {
根據(jù)xsd序列化與反序列化xml                                mySerializer.Serialize(stream, obj);
根據(jù)xsd序列化與反序列化xml                        }
根據(jù)xsd序列化與反序列化xml                }
根據(jù)xsd序列化與反序列化xml        }

4,序列化與反序列化
根據(jù)xsd序列化與反序列化xml//反序列化
根據(jù)xsd序列化與反序列化xml                                Update a = new Update();
根據(jù)xsd序列化與反序列化xml                                a.version = new Version();
根據(jù)xsd序列化與反序列化xml                                a.version.type = "0";
根據(jù)xsd序列化與反序列化xml                                a.version.value = "1.0.0.0";
根據(jù)xsd序列化與反序列化xml                                a.updatetime = new Updatetime();
根據(jù)xsd序列化與反序列化xml                                a.updatetime.value = new System.DateTime();
根據(jù)xsd序列化與反序列化xml                                a.files = new File[1];
根據(jù)xsd序列化與反序列化xml                                a.files[0] = new File();
根據(jù)xsd序列化與反序列化xml                                a.files[0].url = "http://test.exe";
根據(jù)xsd序列化與反序列化xml                                Common.SerializeObjectToXml(a, "目標(biāo).xml", typeof(Update));
根據(jù)xsd序列化與反序列化xml
                                //系列化
根據(jù)xsd序列化與反序列化xml                                Update u = (Update)Common.DeserializeXmlToObject("目標(biāo).xml", typeof(Update));

5,gameover
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI