2010-10-27 31 views
3

我下載了XML Schema for XML Schemas,地址爲http://www.w3.org/2001/XMLSchema.xsd如何在創建XmlSerializer對象時排除此異常?

然後我用XSD.EXE從下載的文件創建一個類。我打電話給班級schema.cs

我再執行這行代碼:

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

,並得到這個錯誤:

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

如何找到重複的元素,並修復它,不破壞模式?

+0

可能是例外有一個行號和位置號碼。你可以添加這些嗎? – 2010-10-27 17:26:59

+0

@Pieter:異常不包括行號,內部異常也沒有。 – 2010-10-27 17:35:48

+0

而你根本沒有提示什麼註釋會拋出?我已經看過XSD,看起來好像有很多有多個註釋的範圍。 – 2010-10-27 17:38:59

回答

0

因爲註釋元素只是註釋,所以您可以嘗試簡單地過濾所有這些。只需首先將XML加載到XDocument並刪除所有註釋元素。

+0

不幸的是,我需要評論。 – 2010-10-27 17:30:30

+0

看到我的評論?你有沒有線路/位置號碼的例外? – 2010-10-27 17:32:38

1

我認爲生成的類有缺陷。

我改變了屬性來修復第一個錯誤,但是發現了新的錯誤。

/// <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; 
    } 
} 
+0

我確實使用了較早版本的XSD,因此我使用您引用的版本再次運行它,並進行了文件比較。這兩個文件之間唯一的區別是CompilerGenerated屬性版本,所以我不費心去測試新文件。奇怪的。我會測試新文件並讓你知道。 – 2010-10-27 18:39:49

+0

我認爲問題仍然存在(關於所有錯誤的中斷已關閉)。我認爲生成的班雖然不好... – eschneider 2010-10-27 18:41:58

+0

是的,它拋出了同樣的例外。 – 2010-10-27 18:47:47