在 C# 中反序列化 XML 时如何获取对父对象的引用?
我似乎无法让它工作,这是我的(精简的)代码: -
[XmlRoot("report")]
public class Report
{
[XmlArray("sections"), XmlArrayItem("section")]
public List<Section> Sections;
}
public class Section
{
public Report Report;
}
我错过了什么吗?
I can't seem to get this working, here is my (stripped down) code: -
[XmlRoot("report")]
public class Report
{
[XmlArray("sections"), XmlArrayItem("section")]
public List<Section> Sections;
}
public class Section
{
public Report Report;
}
Am I missing something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的对象包含 XmlSerializer 不支持的循环引用 类。 您可以改为查看 DataContractSerializer 支持此类方案。
Your objects contain circular references which is not supported by the XmlSerializer class. You could instead look at the DataContractSerializer which supports such scenarios.
您应该确保您知道如何序列化和反序列化这些类。 编写您想要的 XML 结果,并弄清楚您希望对象如何成为 XML,反之亦然。 这不是理所当然的事。
You should make sure you know how you want those classes to serialize and deserialize. Write the XML you want as a result, and figure out how you want objects to become XML and vice versa. It's not a no-brainer.
这是我的解决方案。 它可能不像你想象的那么优雅:
Here's my solution. It might not be as elegant as you'd expect: