XML 序列化/反序列化名称和继承
我在反序列化期间收到以下错误:
类型“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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要为您的子类型指定不同的 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.