如何创建要从 XML 反序列化的类

阿伦甘地

如何创建一个必须用于反序列化 XML 的 C# 类,如下所示

<?xml version="1.0" encoding="utf-8"?>
<XML>
    <StatusCode>-2</StatusCode>
    <Warnings />
    <Errors>
        <Error> Debtor #2 Invalid Postal Code</Error>
        <Error>Invalid lien term</Error>
    </Errors>
</XML>
杰德翁

尝试以下:

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


namespace ConsoleApplication110
{
    class Program
    {
        const string INPUT_FILENAME = @"c:\temp\test.xml";
        const string OUTPUT_FILENAME = @"c:\temp\test1.xml";

        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(INPUT_FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(XML));
            XML xml = (XML)serializer.Deserialize(reader);

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create(OUTPUT_FILENAME, settings);
            serializer.Serialize(writer, xml);
        }
    }
    public class XML
    {
        public int StatusCode { get; set; }
        public string Warnings { get; set; }

        [XmlArray("Errors")]
        [XmlArrayItem("Error")]
        public List<string> errors { get; set; }

    }


}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章