3

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?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501

2 Answers2

1

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;
    }
}
  • I did use an earlier version of XSD, so I ran it again using the version you are referencing, and did a file compare. The only differences between the two files were the CompilerGenerated attribute versions, so I didn't bother to test the new file. Weird. I'll test the new file and let you know. – Robert Harvey Oct 27 '10 at 18:39
  • I think the problem still exists (Break on all errors was off). I think the generated class is bad though... –  Oct 27 '10 at 18:41
  • Yeah, it threw the same exception. – Robert Harvey Oct 27 '10 at 18:47
0

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.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111