c# - XML 序列化不填充数组

标签 c# xml serialization .net-4.6.2

我正在尝试序列化以下 XML。但是“Keys”部分没有被填充,它在序列化对象中显示为 null。

<?xml version="1.0"?>
<Golden>
<SecType>
    <ID>New</ID>
    <Count>1</Count>
</SecType>
<Request>
    <Action>New</Action>
    <Keys>
        <Key>
            <ReferenceType>AAA</ReferenceType>
            <ReferenceValue>1111</ReferenceValue>
            <Description></Description>
        </Key>
        <Key>
            <ReferenceType>BBBB</ReferenceType>
            <ReferenceValue>22222</ReferenceValue>
            <Description></Description>
        </Key>
    </Keys>
</Request></Golden>

我正在尝试创建一个自定义类对象以供进一步使用。请在下面查看我的代码。

[Serializable]
[XmlRootAttribute("Golden")]
public class Process
{
    [XmlElementAttribute("SecType")]
    public SecType secType { get; set; }
    [XmlElementAttribute("Request")]
    public Request request { get; set; }
}

[Serializable]
[XmlRootAttribute("SecType")]
public class SecType 
{
    [XmlElementAttribute("ID")]
    public string ID { get; set; }
    [XmlElementAttribute("Count")]
    public int Count { get; set; }
}

[Serializable]
[XmlRootAttribute("Request")]
public class Request
{
    [XmlElementAttribute("Action")]
    public string Action { get; set; }
    [XmlElementAttribute("Keys")]
    public Keys keys { get; set; }
}

[Serializable()]
[XmlRootAttribute("Keys")]
public class Keys
{
   [XmlArray("Keys")]
    [XmlArrayItem("Key", typeof(Key))]
    public Key[] key { get; set; }
}

[Serializable]
[XmlRootAttribute("Key")]
public class Key
{
    [XmlElementAttribute("ReferenceType")]
    public string ReferenceType { get; set; }
    [XmlElementAttribute("ReferenceValue")]
    public string ReferenceValue { get; set; }
    [XmlElementAttribute("Description")]
    public string Description { get; set; }
}


    string sPath = @"C:\Test\ConsoleApp1\test.xml";
    Process proc = new Process();
    XmlSerializer serializer = new XmlSerializer(typeof(Process));
    StreamReader reader = new StreamReader(sPath);
    proc = (Process)serializer.Deserialize(reader);
    reader.Close();

我主要引用this .但它在我的实现中不起作用。谢谢你的帮助

最佳答案

我刚刚修复了关键元素:

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

namespace ConsoleApplication142
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(Process));
            Process proc = (Process)serializer.Deserialize(reader);
        }
    }
    [Serializable]
    [XmlRootAttribute("Golden")]
    public class Process
    {
        [XmlElementAttribute("SecType")]
        public SecType secType { get; set; }
        [XmlElementAttribute("Request")]
        public Request request { get; set; }
    }

    [Serializable]
    [XmlRootAttribute("SecType")]
    public class SecType 
    {
        [XmlElementAttribute("ID")]
        public string ID { get; set; }
        [XmlElementAttribute("Count")]
        public int Count { get; set; }
    }

    [Serializable]
    [XmlRootAttribute("Request")]
    public class Request
    {
        [XmlElementAttribute("Action")]
        public string Action { get; set; }
        [XmlArray("Keys")]
        [XmlArrayItem("Key")]
        public Key[] keys { get; set; }
    }

    [Serializable]
    [XmlRootAttribute("Key")]
    public class Key
    {
        [XmlElementAttribute("ReferenceType")]
        public string ReferenceType { get; set; }
        [XmlElementAttribute("ReferenceValue")]
        public string ReferenceValue { get; set; }
        [XmlElementAttribute("Description")]
        public string Description { get; set; }
    }


}

关于c# - XML 序列化不填充数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58735696/

相关文章:

c# - MVC 通过表单集合获取 Basic RadioButton 检查的属性

c# - 当我在我的 asp.net mvc 项目上以 Debug模式启动浏览器时,我可以告诉 VS2013 将其直接导航到站点的某个部分吗?

php - 如何从 Magento-1 价格中删除小数?

c - 使用c套接字传输音频文件

c++ - 使用 Boost Serialization 注册用户提供的派生类型

java - Apache Thrift C GLib -> Java 反序列化问题

c# - 将 Excel 导入 Datagridview

c# - 仅在 Vista/Windows Server 2008 上出现 P/Invoke 错误

另一个文件中可能值的 XML 模式枚举

java - 在java中写入XML文件时出错