gpt4 book ai didi

c# - 如何使用 XmlSerializer 序列化对象集合

转载 作者:数据小太阳 更新时间:2023-10-29 02:07:30 26 4
gpt4 key购买 nike

以下是我迄今为止为处理由第 3 方 DLL 返回的 XmlDocument 而采取的步骤。

  1. 我将 XmlDocument 保存为 SegmentationSummary.xml。
  2. 我使用 XSD.exe 创建了 SegmentationSummary.xsd。
  3. 我使用 XSD.exe 创建了 SegmentationSummary.cs。

这是 SegmentationSummary.cs 的示例。请注意,ShmResult 是根节点表示。

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "omitted")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "omitted", IsNullable = false)]
public partial class ShmResult
{
private ShmResultDownloadDetail[] downloadDetailField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DownloadDetail")]
public ShmResultDownloadDetail[] DownloadDetail
{
get
{
return this.downloadDetailField;
}
set
{
this.downloadDetailField = value;
}
}
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "omitted")]
public partial class ShmResultDownloadDetail
{
private string modelCodeField;

/// <remarks/>
public string ModelCode
{
get
{
return this.modelCodeField;
}
set
{
this.modelCodeField = value;
}
}
}

现在,我想用它来读取 XmlDocument 并开始使用 SegmentationSummary.cs 中的类。这是我写的代码:

private XmlDocument _document;
SegmentationSummary.ShmResult _Result;
private void LoadXML()
{
XmlReader xmlRdr = new XmlNodeReader(_document);
System.Xml.Serialization.XmlSerializer s = new System.Xml.Serialization.XmlSerializer(typeof(SegmentationSummary.ShmResult));
_Result = (SegmentationSummary.ShmResult)s.Deserialize(xmlRdr);
}

当执行 LoadXML() 时,我得到了以下各种异常:

Test method SegmentationSummaryHandlerTest.TestMethod1 threw exception: System.InvalidOperationException: Unable to generate a temporary class (result=1). error CS0030: Cannot convert type 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail[]' to 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail' error CS0029: Cannot implicitly convert type 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail' to 'MERC.AIRCAT.SHMCoreInterface.SegmentationSummary.ShmResultDownloadDetail[]'

现在,常见问题解答位于 http://msdn.microsoft.com/en-us/library/ms950721.aspx说明如下:

Q: How do I serialize collections of objects?

A: The XmlSerializer throws an exception when the collection contains types that were not declared to the constructor of the XmlSerializer. You can:

  1. Declare the types to the serializer by passing in a Type[] with the types to expect within the collection.

    OR

  2. Implement a strongly-typed collection derived from System.Collections.CollectionBase with an indexer matching the Add() method.

我的问题是:其中哪一个是“最好的”,我该如何着手实现该解决方案?

最佳答案

我遇到了类似的问题。

序列化嵌套的未绑定(bind)元素存在问题。 Here is an explanation.

为了解决这个问题,我从 xsd 中删除了 maxoccurs="unbounded"并重新生成了类文件。之后序列化就成功了。

关于c# - 如何使用 XmlSerializer 序列化对象集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1643312/

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