XML 序列化/反序列化名称和继承

发布于 2025-01-01 07:26:46 字数 682 浏览 0 评论 0原文

我在反序列化期间收到以下错误:

类型“MyLib.Xml.CompanyDetails”和“MyApp.Web.Models.CompanyDetails”都使用命名空间“http://xml.domain”中的 XML 类型名称“CompanyDetails”。 com/schema'。使用 XML 属性为类型指定唯一的 XML 名称和/或命名空间。

我可以使用不同的类名来解决问题,如下所示,但我不想...

[XmlRoot(ElementName = "CompanyDetails", Namespace = "http://xml.domain.com/schema/schema")]
public class CompanyDetailsXX : MyLib.Xml.CompanyDetails

我使用继承,因为我无法更改基类并且需要添加额外的属性(在反序列化期间不填充)

我希望基类和父类具有相同的名称,因为它很有意义,例如:

[XmlRoot(ElementName = "CompanyDetails", Namespace = "http://xml.domain.com/schema/schema")]
    public class CompanyDetails : MyLib.Xml.CompanyDetails

有什么想法吗?

I'm getting the following error during deserialization:

Types 'MyLib.Xml.CompanyDetails' and 'MyApp.Web.Models.CompanyDetails' both use the XML type name, 'CompanyDetails', from namespace 'http://xml.domain.com/schema'. Use XML attributes to specify a unique XML name and/or namespace for the type.

I can fix the issue using different class names as show below, but I don't want to...

[XmlRoot(ElementName = "CompanyDetails", Namespace = "http://xml.domain.com/schema/schema")]
public class CompanyDetailsXX : MyLib.Xml.CompanyDetails

I use inheritance as I'm not able to change the base class and need to add extra properties (not populated during deserialization)

I want the base and parent class to have the same names, as it makes sense like:

[XmlRoot(ElementName = "CompanyDetails", Namespace = "http://xml.domain.com/schema/schema")]
    public class CompanyDetails : MyLib.Xml.CompanyDetails

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟雨扶苏 2025-01-08 07:26:46

您需要为您的子类型指定不同的 XML 元素名称。

您在 C# 中为您的子类创建了不同的类名,不是吗?同样的要求也适用于 XML 类型。 XML 子类型不能与其父类型同名。

当父对象和子对象都被流式传输到 XML 时尤其如此,正如您发布的错误消息所示。

You need to specify a different XML element name for your child type.

You created a different class name for your child class in C#, didn't you? The same requirement applies to XML types. An XML child type cannot have the same name as its parent.

This is especially true when both the parent and the child objects are being streamed out to XML, as seems to be indicated by the error message you posted.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文