C# XML to Class(Object/Serialization)
C# XML to Class(Object/Serialization)
資料來源: https://json2csharp.com/xml-to-csharp
code:
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace CS_XML2Class { //https://json2csharp.com/xml-to-csharp [XmlRoot(ElementName = "value")] public class Value { [XmlElement(ElementName = "string")] public string String { get; set; } [XmlElement(ElementName = "array")] public Array Array { get; set; } } [XmlRoot(ElementName = "data")] public class Data { [XmlElement(ElementName = "value")] public List<Value> Value { get; set; } } [XmlRoot(ElementName = "array")] public class Array { [XmlElement(ElementName = "data")] public Data Data { get; set; } } [XmlRoot(ElementName = "param")] public class Param { [XmlElement(ElementName = "value")] public Value Value { get; set; } } [XmlRoot(ElementName = "params")] public class Params { [XmlElement(ElementName = "param")] public Param Param { get; set; } } [XmlRoot(ElementName = "methodResponse")] public class MethodResponse { [XmlElement(ElementName = "params")] public Params Params { get; set; } } class Program { static void Pause() { Console.Write("Press any key to continue..."); Console.ReadKey(true); } static void Main(string[] args) { String StrData = "<?xml version=\"1.0\" encoding=\"UTF - 8\"?><methodResponse><params><param><value><array><data><value><string>OK</string></value><value><string>DA7921811431685FABE2A399FD465F8F</string></value><value><string>lkYBJXfZj7kmrDDsF3uqkzsc7+Zgtkl/YU82XvvXsFyBtOZwIOFeP4d1ECnqLKkcf5Xh0NpKxidOTUzgD//DZGPFLtMXJGxujjNRkfxL6/wcB0HEPYvJxMOcMgmGMtlhFbnEejeeKqXdy/wYlUdWGKsSW1+m2SxJbtEffqfUzVTXufqunqP1IaHSm6QloGa6pa18Swtyti1zvOMkeL+wNGQOcrInOQnxeBg2fbzHZwwakpQtgmdQTrnstbTtpchIYQh4IXSzy0h47gtgjn48UmFkAJtTlY5phlL8LAloSTwkUdMZtJjVBu0bn5AAVV7AGl3BqYYjngn+goAG4ef/W+dYgmdaKVFqUGPDHZixmzUw64xfmeFWMMkOrm8NBOzNeHyf0eDYaX4gvGBIPuVGOg5RuARk3S7/6nvm/EMbS7wOMUXmWM0QtMhbMCtxEBgKPSpCvSTty4kZ2ZW7YY6GWLFdDm0eUB2/XFNI3PgGCpQZmEPRDgppUKiPUAefLXcthubrRTTexJJuyBfF7s2sKvOAZJYIlBP5OSsHAaWWNJQ9pZdGqlndokunYrazvApp6cyRr3icp5Lf6oxkzVV1jjb3An38Jwebd4Ai7YRPDXcQLm6DFt6SrcIysl0BXUzRDK8iay9rEc1vOQVcBZJhZxAPArjrknRhVTOF5BPraz8=</string></value><value><string>LAnDFyzYC9z0fm6NxMP/TlDfiHHXvfU/kwZXCbqeqSKAIWjea84zgYmA9KwDsNL9w1IdGnKFlv3Qg53MxGjLEs1yEXIpwBrc/N6XV8aVsUjK+0KHxMmiY/7i8AmStHbERdfdz5BLo712ZR3IpE1e/wvoqYjtHE3ggGUNEX6k0tf6AvOo64jvLYVZJdNqw1cVhXIaVoWrHhSj4ABqi+9SsMdua6ghH1e3GPOi7cOVLzn+5H+Eizki7ZxSUilUI/7nIIw2711zDouawlG052U3vyU2rO8DXmr3qsws/Ys1/pWS3+woMrzfZxYZlhzdCLAWw0V5VrcbkMhmhN6NybXj3Xu6yd7BVUf9obb882x0zuglYbhujSjTAX2yJQYV+T4G9uKyRQUP7TA/Kse0z7mBQhzxYULj2M40W4Tt1d8H6Y/XBHZmcER3ojko9nnpqC3IsgFWUJL6C+HmJ8ESYKse8dEv3tfB+ba5RxKYmEGio+r4xLA70gWwCRN1zsh+qTUf+WerHgFkUT1uXNrvhfntkTPZwHd0nZ0xGpenkfq4y8FM2aridZayu14P+5AFSLnaKA9qHZuLzapQlc1c8fHY78MX1JUkf5eu8pTK3NhMWPzoQTWx+FttH/pXtITw9G4thMtF8YRiNRxfPassF0aQ0YMeF0/cQ1zO9zmTSIfkpNg=</string></value></data></array></value></param></params></methodResponse>"; XmlSerializer serializer = new XmlSerializer(typeof(MethodResponse)); using (StringReader reader = new StringReader(StrData)) { MethodResponse test = (MethodResponse)serializer.Deserialize(reader); for (int i = 0; i < test.Params.Param.Value.Array.Data.Value.Count; i++) { Console.WriteLine("{0}\n", test.Params.Param.Value.Array.Data.Value[i].String); } } Pause(); } } }