创建 XmlSerializer 对象时如何解决此异常?

发布于 2024-09-29 00:23:13 字数 492 浏览 1 评论 0原文

我在 http://www.w3.org/2001/XMLSchema 下载了 XML Schema 的 XML Schema。 xsd

然后,我使用 XSD.EXE 从下载的文件创建一个类。我将该类命名为 schema.cs

然后我执行了这行代码:

XmlSerializer serializer = new XmlSerializer(typeof(schema));

并收到此错误:

XML 元素“注释”来自 名称空间 'http://www.w3.org/2001/XMLSchema' 是 已存在于当前范围内。

如何在不破坏架构的情况下找到重复的元素并修复它?

I downloaded the XML Schema for XML Schemas at http://www.w3.org/2001/XMLSchema.xsd.

I then used XSD.EXE to create a class from the downloaded file. I called the class schema.cs.

I then executed this line of code:

XmlSerializer serializer = new XmlSerializer(typeof(schema));

and got this error:

The XML element 'annotation' from
namespace
'http://www.w3.org/2001/XMLSchema' is
already present in the current scope.

How do I find the duplicate element and fix it, without breaking the schema?

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

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

发布评论

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

评论(2

醉梦枕江山 2024-10-06 00:23:13

我认为生成的类有缺陷。

我更改了属性以修复第一个错误,但发现了一个新错误。

/// <remarks/>
//[System.Xml.Serialization.XmlElementAttribute("annotation", typeof(annotation))]
[System.Xml.Serialization.XmlElementAttribute("Items", typeof(annotation))]
[System.Xml.Serialization.XmlElementAttribute("import", typeof(import))]
[System.Xml.Serialization.XmlElementAttribute("include", typeof(include))]
[System.Xml.Serialization.XmlElementAttribute("redefine", typeof(redefine))]
public openAttrs[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}

I think the generated class has flaws.

I changed the attribute to fix the first error but a new error is discovered.

/// <remarks/>
//[System.Xml.Serialization.XmlElementAttribute("annotation", typeof(annotation))]
[System.Xml.Serialization.XmlElementAttribute("Items", typeof(annotation))]
[System.Xml.Serialization.XmlElementAttribute("import", typeof(import))]
[System.Xml.Serialization.XmlElementAttribute("include", typeof(include))]
[System.Xml.Serialization.XmlElementAttribute("redefine", typeof(redefine))]
public openAttrs[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}
浅唱ヾ落雨殇 2024-10-06 00:23:13

因为注释元素只是注释,所以您可以尝试简单地过滤掉所有这些元素。只需首先将 XML 加载到 XDocument 中并删除所有注释元素。

Because the annotate elements are just comments, you could try simply filtering all of these out. Just first load the XML into an XDocument and remove all annotate elements.

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