C# xml的序列化与反序列化
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XMLTest { public class Station { [XmlElement("id")] public int id { get; set; } [XmlElement("name")] public string name { get; set; } [XmlElement("api_key")] public string api_key { get; set; } [XmlElement("site_id")] public int site_id { get; set; } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Serialization; namespace XMLTest { [XmlRoot("stations")] public class Stations { private string _type; [XmlAttribute("type")] public string Type { get { return _type; } set { _type = value; } } [XmlElement("station")] public List<Station> stations { get; set; } } }
string xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><stations type=\"array\"><station><id type=\"integer\">647</id><name>CU-BT-C1</name><api_key>4d1da4be</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">642</id><name>CU-BT-D0</name><api_key>8ab009a6</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">649</id><name>CU-BT-F2</name><api_key>31a8c368</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">926</id><name>CU-DEFECT-LOGS</name><api_key>c251f77c</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">640</id><name>CU-FT-P17</name><api_key>b44b352d</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">641</id><name>CU-FT-Q18</name><api_key>890b6cb2</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">652</id><name>CU-FT-R16</name><api_key>0ed639fc</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">650</id><name>CU-FT-S17 (changed to P17)</name><api_key>f04629f7</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">651</id><name>CU-FT-T15</name><api_key>d68d1b0f</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">933</id><name>CU-FT-U11</name><api_key>c982482d</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">658</id><name>CU-G2-FT-E5</name><api_key>d440f52a</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">659</id><name>CU-G2-FT-F6</name><api_key>35f89a68</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">676</id><name>CU-G2-FT-H4</name><api_key>f1b50acd</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">667</id><name>CU-G2-MB-0</name><api_key>38dc4eee</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">657</id><name>CU-G2-MB-11</name><api_key>bf9ec146</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">668</id><name>CU-G2-MB-13</name><api_key>429482c9</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">672</id><name>CU-G2-MB-14</name><api_key>43383e86</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">671</id><name>CU-G2-MB-15</name><api_key>ccb15ca6</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">670</id><name>CU-G2-MB-16</name><api_key>d0e4ff10</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">674</id><name>CU-G2-MB-D14</name><api_key>4a1c34ad</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">944</id><name>CU-NS-BT-03</name><api_key>8bfdb0f2</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">711</id><name>CU-NS-BT-04</name><api_key>94ef445e</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">998</id><name>CU-NS-BT-05</name><api_key>97b7b443</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1032</id><name>CU-NS-BT-07</name><api_key>6f48aff8</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1169</id><name>CU-NS-BT-08</name><api_key>c46aa4e1</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1114</id><name>CU-NS-BT-09</name><api_key>fbee2d8e</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">2262</id><name>CU-NS-BT-10</name><api_key>904dad13</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">959</id><name>CU-NS-FAT-03</name><api_key>5f031b2d</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">964</id><name>CU-NS-FAT-04</name><api_key>cea3ed2b</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">999</id><name>CU-NS-FAT-05</name><api_key>fc2b899b</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1033</id><name>CU-NS-FAT-07</name><api_key>761eb890</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1267</id><name>CU-NS-iFAT-02</name><api_key>0a253401</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1395</id><name>CU-NS-iFAT-04</name><api_key>5892b0ca</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1396</id><name>CU-NS-iFAT-05</name><api_key>571d6db5</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">1008</id><name>CU-NS-Packaging</name><api_key>581d6c51</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">966</id><name>CU-Packaging</name><api_key>1b682cc0</api_key><site_id type=\"integer\">88</site_id></station><station><id type=\"integer\">814</id><name>CU-SERVER-TEST</name><api_key>502808e6</api_key><site_id type=\"integer\">88</site_id></station></stations>"; Stations stations1 = Deserialize<Stations>(xmlStr); Station sta1 = new Station(); sta1.site_id = 1; sta1.name = "test1"; sta1.id = 1; sta1.api_key = "4324234324"; Station sta2 = new Station(); sta2.site_id = 2; sta2.name = "test2"; sta2.id = 2; sta2.api_key = "tretetetr"; Stations stations = new Stations(); stations.Type = "array"; stations.stations = new List<Station>(); stations.stations.Add(sta1); stations.stations.Add(sta2); string xmlResult1 = Serializer<Stations>(stations);
xml:
<?xml version="1.0" encoding="UTF-8"?> <stations type="array"> <station> <id type="integer">647</id> <name>CU-BT-C1</name> <api_key>4d1da4be</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">642</id> <name>CU-BT-D0</name> <api_key>8ab009a6</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">649</id> <name>CU-BT-F2</name> <api_key>31a8c368</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">926</id> <name>CU-DEFECT-LOGS</name> <api_key>c251f77c</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">640</id> <name>CU-FT-P17</name> <api_key>b44b352d</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">641</id> <name>CU-FT-Q18</name> <api_key>890b6cb2</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">652</id> <name>CU-FT-R16</name> <api_key>0ed639fc</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">650</id> <name>CU-FT-S17 (changed to P17)</name> <api_key>f04629f7</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">651</id> <name>CU-FT-T15</name> <api_key>d68d1b0f</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">933</id> <name>CU-FT-U11</name> <api_key>c982482d</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">658</id> <name>CU-G2-FT-E5</name> <api_key>d440f52a</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">659</id> <name>CU-G2-FT-F6</name> <api_key>35f89a68</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">676</id> <name>CU-G2-FT-H4</name> <api_key>f1b50acd</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">667</id> <name>CU-G2-MB-0</name> <api_key>38dc4eee</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">657</id> <name>CU-G2-MB-11</name> <api_key>bf9ec146</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">668</id> <name>CU-G2-MB-13</name> <api_key>429482c9</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">672</id> <name>CU-G2-MB-14</name> <api_key>43383e86</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">671</id> <name>CU-G2-MB-15</name> <api_key>ccb15ca6</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">670</id> <name>CU-G2-MB-16</name> <api_key>d0e4ff10</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">674</id> <name>CU-G2-MB-D14</name> <api_key>4a1c34ad</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">944</id> <name>CU-NS-BT-03</name> <api_key>8bfdb0f2</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">711</id> <name>CU-NS-BT-04</name> <api_key>94ef445e</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">998</id> <name>CU-NS-BT-05</name> <api_key>97b7b443</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1032</id> <name>CU-NS-BT-07</name> <api_key>6f48aff8</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1169</id> <name>CU-NS-BT-08</name> <api_key>c46aa4e1</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1114</id> <name>CU-NS-BT-09</name> <api_key>fbee2d8e</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">2262</id> <name>CU-NS-BT-10</name> <api_key>904dad13</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">959</id> <name>CU-NS-FAT-03</name> <api_key>5f031b2d</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">964</id> <name>CU-NS-FAT-04</name> <api_key>cea3ed2b</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">999</id> <name>CU-NS-FAT-05</name> <api_key>fc2b899b</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1033</id> <name>CU-NS-FAT-07</name> <api_key>761eb890</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1267</id> <name>CU-NS-iFAT-02</name> <api_key>0a253401</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1395</id> <name>CU-NS-iFAT-04</name> <api_key>5892b0ca</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1396</id> <name>CU-NS-iFAT-05</name> <api_key>571d6db5</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">1008</id> <name>CU-NS-Packaging</name> <api_key>581d6c51</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">966</id> <name>CU-Packaging</name> <api_key>1b682cc0</api_key> <site_id type="integer">88</site_id> </station> <station> <id type="integer">814</id> <name>CU-SERVER-TEST</name> <api_key>502808e6</api_key> <site_id type="integer">88</site_id> </station> </stations>