gpt4 book ai didi

c# - DataContractSerializer 和通用列表

转载 作者:太空宇宙 更新时间:2023-11-03 20:04:31 24 4
gpt4 key购买 nike

我在反序列化 xml 文件中的列表时遇到问题。我的属性是内部的,所以我使用的是 datacontractserializer 而不是 xmlserializer。

我的xml如下

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Root">
<BackgroundColor>
0,0,255
</BackgroundColor>
<RowGap>1</RowGap>
<Table>
<TableHeading>"H1"</TableHeading>
<StartingColumn>A</StartingColumn>
</Table>
<Table>
<TableHeading>"H2"</TableHeading>
<StartingColumn>B</StartingColumn>
</Table>
</Root>

根有表列表。 ListOfTables 未被反序列化。类是

[DataContract()]
public class Root
{
[OnDeserialized]
internal void OnSerializingMethod(StreamingContext context)
{
if (Table == null)
{
Table = new List<TableStructure>();
}
}

[DataMember(Name = "RowGap")]
internal int RowGap { get; set; }

[DataMember(Name = "TableHeaderBackgroundColor")]
internal string HdrBackColor { get; set; }

[DataMember()]
internal List<TableStructure> Table { get; set; }
}

[DataContract(Name = "Table", Namespace = "")]
public sealed class TableStructure
{
[DataMember]
public string TableHeading { get; set; }

[DataMember]
public string StartingColumn { get; set; }
}

反序列化方法

internal static Root GetSettings()
{
Root settings;
using (FileStream fs = new FileStream("Root.xml", FileMode.Open, FileAccess.Read))
{
using (XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas()))
{
var x = new DataContractSerializer(typeof(Root));
settings = (Root)x.ReadObject(reader);
reader.Close();
fs.Close();
}
}
return settings;
}

这执行没有任何错误,但不加载任何表信息。正在加载 RowGap 和颜色

最佳答案

我修改了您的解决方案以使其工作(也添加了 XML 命名空间):

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/Root")]
public class Root
{
[OnDeserialized]
internal void OnSerializingMethod(StreamingContext context)
{
if (Table == null)
{
Table = new List<TableStructure>();
}
}

[DataMember(Name = "RowGap")]
internal int RowGap { get; set; }

[DataMember(Name = "TableHeaderBackgroundColor")]
internal string HdrBackColor { get; set; }

[DataMember(Name = "Tables")]
internal List<TableStructure> Table { get; set; }
}

[DataContract(Name = "Table", Namespace = "http://schemas.datacontract.org/2004/07/Root")]
public sealed class TableStructure
{
[DataMember(Name = "StartingColumn")]
public string TableHeading { get; set; }

[DataMember(Name = "TableHeading")]
public string StartingColumn { get; set; }
}

XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<Root xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/Root">
<BackgroundColor>
0,0,255
</BackgroundColor>
<RowGap>1</RowGap>
<!-- Used as container element for list -->
<Tables>
<Table>
<!-- Sorted elements alphabetically -->
<StartingColumn>A</StartingColumn>
<TableHeading>"H1"</TableHeading>
</Table>
<Table>
<!-- Sorted elements alphabetically -->
<StartingColumn>B</StartingColumn>
<TableHeading>"H2"</TableHeading>
</Table>
</Tables>
</Root>

注意:DataContractSerializer 会考虑 XML 元素的顺序,如下所述:http://social.msdn.microsoft.com/Forums/vstudio/en-US/a891928b-d27a-4ef2-83b3-ee407c6b9187/order-of-data-members-in-the-xml-string-influences-deserialization-datacontractserializer?forum=wcf .由于您没有按字母顺序对元素进行排序,我修改了 XML 文件以满足 DataContractSerializer 的行为。

您可以使用 DataMember 指定 XML 元素的顺序属性及其 DataMember.Order属性(property)。

关于c# - DataContractSerializer 和通用列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24652839/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com