开发者

How do I troubleshoot this exception when creating an XmlSerializer object?

开发者 https://www.devze.com 2023-01-22 10:20 出处:网络
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 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 XM开发者_运维问答L 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?


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;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消