c# - 在类中使用数组进行 XML 反序列化

标签 c# arrays xml deserialization

我正在尝试反序列化一串 XML 数据。但是我在反序列化函数返回的对象中没有返回任何数据。

类定义如下:

[XmlType("STATE-COUNTY-RECORDS")]
public class StateCountyRecords
{
    [XmlElement("TOTAL")]
    public int Total { get; set; }

    [XmlElement("LIMIT")]
    public int Limit { get; set; }

    [XmlElement("OFFSET")]
    public int Offset { get; set; }

    [XmlArray("STATE-COUNTY-ARRAY"), XmlArrayItem("STATE-COUNTY")]
    public StateCounty[] StateCountyArray { get; set; }

    public StateCountyRecords() {}

    public StateCountyRecords(int total, int limit, int offset, int[] id, string[] fips_code, string[] state, string[] name)
    {
        Total = total;
        Limit = limit;
        Offset = offset;

        var count = id.Length;
        StateCountyArray = new StateCounty[count];
        for (int i = 0; i < count; i++)
        {
            StateCountyArray[i] = new StateCounty(id[i], fips_code[i], state[i], name[i]);
        }
    }
}

public class StateCounty
{
    [XmlElement("ID")]
    public int Id { get; set; }

    [XmlElement("FIPS-CODE")]
    public string Fips_Code { get; set; }

    [XmlElement("STATE")]
    public string State { get; set; }

    [XmlElement("NAME")]
    public string Name { get; set; }

    public StateCounty(int id, string fips_code, string state, string name)
    {
        Id = id;
        Fips_Code = fips_code;
        State = state;
        Name = name;
    }

    public StateCounty() { }
}

这是 xml 字符串数据:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<STATE-COUNTY-FILE xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
<STATE-COUNTY-RECORDS>
    <TOTAL>3314</TOTAL>
    <LIMIT>10</LIMIT>
    <OFFSET>0</OFFSET>
    <STATE-COUNTY-ARRAY>
    <STATE-COUNTY>
        <ID>1</ID>
        <FIPS-CODE>01000</FIPS-CODE>
        <STATE>AL</STATE>
        <NAME>Alabama</NAME>
    </STATE-COUNTY>
    <STATE-COUNTY>
        <ID>2</ID>
        <FIPS-CODE>01001</FIPS-CODE>
        <STATE>AL</STATE>
        <NAME>Autauga</NAME>
    </STATE-COUNTY>
    <STATE-COUNTY>
        <ID>3</ID>
        <FIPS-CODE>01003</FIPS-CODE>
        <STATE>AL</STATE>
        <NAME>Baldwin</NAME>
    </STATE-COUNTY>
    </STATE-COUNTY-ARRAY>
</STATE-COUNTY-RECORDS>
</STATE-COUNTY-FILE>

这是反序列化xml字符串的函数:

    public static StateCountyRecords DeserializeStateCountyData(string xmlString)
    {
        XmlSerializer mySerializer = new XmlSerializer(typeof(StateCountyRecords), new XmlRootAttribute("STATE-COUNTY-FILE"));
        using (XmlReader myXmlReader = XmlReader.Create(new StringReader(xmlString)))
        {
            StateCountyRecords rslt;
            rslt = (StateCountyRecords)mySerializer.Deserialize(myXmlReader);
        }
    }
}

返回的rslt中没有Total和Limit的数据,Offset均为0,StateCountyArray为null。我没有收到任何错误。我通过以下方式确保 xml 有效:

if (myXmlReader.CanResolveEntity && myXmlReader.CanReadValueChunk && myXmlReader.CanReadBinaryContent)
                rslt = (StateCountyRecords)mySerializer.Deserialize(myXmlReader);

我想知道我的类定义的 XmlElement 是否与 XML 文件不匹配。

最佳答案

您的问题是您的 XML 有一个数据模型中未考虑的外部元素,即 <STATE-COUNTY-FILE>元素:

<STATE-COUNTY-FILE xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <STATE-COUNTY-RECORDS>
    <!-- Various elements under StateCountyRecords -->
  </STATE-COUNTY-RECORDS>
</STATE-COUNTY-FILE>

因为您的数据模型的根是 StateCountyRecords没有任何东西对应于这个最外层的元素。您尝试使用 new XmlRootAttribute("STATE-COUNTY-FILE") 来解决这个问题,但是这只会重命名外部元素,不会添加额外的嵌套级别。

相反,您需要为此引入一个外部容器 POCO:

[XmlType("STATE-COUNTY-FILE")]
public class StateCountyFile
{
    [XmlElement("STATE-COUNTY-RECORDS")]
    public StateCountyRecords StateCountyRecords { get; set; }
}

反序列化如下:

    public static StateCountyRecords DeserializeStateCountyData(string xmlString)
    {
        var mySerializer = new XmlSerializer(typeof(StateCountyFile));
        using (var myXmlReader = XmlReader.Create(new StringReader(xmlString)))
        {
            var rslt = (StateCountyFile)mySerializer.Deserialize(myXmlReader);
            return rslt.StateCountyRecords;
        }
    }

或者,您可以手动推进 XmlReader直到遇到一个名为 <STATE-COUNTY-RECORDS> 的元素,并反序列化:

    public static StateCountyRecords DeserializeStateCountyData(string xmlString)
    {
        return XmlTypeExtensions.DeserializeNestedElement<StateCountyRecords>(xmlString);
    }

使用扩展方法:

public static class XmlTypeExtensions
{
    public static T DeserializeNestedElement<T>(string xmlString)
    {
        var mySerializer = new XmlSerializer(typeof(T));
        var typeName = typeof(T).XmlTypeName();
        var typeNamespace = typeof(T).XmlTypeNamespace();

        using (var myXmlReader = XmlReader.Create(new StringReader(xmlString)))
        {
            while (myXmlReader.Read())
                if (myXmlReader.NodeType == XmlNodeType.Element && myXmlReader.Name == typeName && myXmlReader.NamespaceURI == typeNamespace)
                {
                    return (T)mySerializer.Deserialize(myXmlReader);
                }
            return default(T);
        }
    }

    public static string XmlTypeName(this Type type)
    {
        var xmlType = type.GetCustomAttribute<XmlTypeAttribute>();
        if (xmlType != null && !string.IsNullOrEmpty(xmlType.TypeName))
            return xmlType.TypeName;
        return type.Name;
    }

    public static string XmlTypeNamespace(this Type type)
    {
        var xmlType = type.GetCustomAttribute<XmlTypeAttribute>();
        if (xmlType != null && !string.IsNullOrEmpty(xmlType.Namespace))
            return xmlType.Namespace;
        return string.Empty;
    }
}

顺便说一句,通过在内存中创建您的类的实例、对其进行序列化,然后将生成的 XML 与输入 XML 进行比较,可以轻松诊断此类反序列化问题。通常问题很明显。

关于c# - 在类中使用数组进行 XML 反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38533367/

相关文章:

python - 复杂的 numpy 数组乘法

javascript - 将动态 XML 内容检索到 Perl 中的变量中

php - 有逗号时拆分数组值

java - 在 "firstUse"之后 onCreate 报告的 Android 首选项设置不一致——我的错,但是怎么样?

java - 针对 XSD java 验证 XML

c# - 如何隐藏或显示 Asp :Content using a Session in If statement

c# - 在类中存储不同泛型类型的列表

c# - 将 List<T> 转换为嵌套 Dictionary<string, Dictionary<string, T>

c# - EF Core 2.1 查询 - 左连接

python - 如何为 DNA 序列生成一个热编码?