c# – 使用XSD2Code生成的代码加载XML

我无法获取由XSD2Code生成的代码来加载一个假定有效的xml文件.

我的测试用例涉及为KML version 2.2.0 schema生成C#代码并尝试使用它加载sample KML file.

此测试期间发生的异常如下:

System.InvalidOperationException : There was an error reflecting type ‘XSDTest.KmlType’.
—-> System.InvalidOperationException : There was an error reflecting property ‘NetworkLinkControl’.
—-> System.InvalidOperationException : There was an error reflecting type ‘XSDTest.NetworkLinkControlType’.
—-> System.InvalidOperationException : There was an error reflecting property ‘Update’.
—-> System.InvalidOperationException : There was an error reflecting type ‘XSDTest.UpdateType’.
—-> System.InvalidOperationException : There was an error reflecting property ‘Items’.
—-> System.InvalidOperationException : There was an error reflecting type ‘XSDTest.DeleteType’.
—-> System.InvalidOperationException : There was an error reflecting property ‘Items’.
—-> System.InvalidOperationException : There was an error reflecting type ‘XSDTest.PlacemarkType’.
—-> System.InvalidOperationException : There was an error reflecting property ‘author’.
—-> System.InvalidOperationException : There was an error reflecting type ‘XSDTest.atomPersonConstruct’.
—-> System.InvalidOperationException : There was an error reflecting property ‘Items’.
—-> System.InvalidOperationException : Type of choice identifier ‘ItemsElementName’ is inconsistent with type of ‘Items’. Please use array of System.Collections.Generic.List`1[[XSDTest.ItemsChoiceType, XSDTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]].

在自动生成的代码中创建XML序列化程序时会发生这种情况:

new System.Xml.Serialization.XmlSerializer(typeof(KmlType))

使用XSD2Code创建代码时,我只将3个valeus从其默认值false更改为true:Serialization.Enabled,Serialization.EnableEncoding和Serialization.GenerateXmlAttributes.

关于使用C#加载KML,我不是在寻找替代方案,但我正在研究一种从有效模式自动生成C#代码的方法.它不需要是XSD2Code,所以如果有更合适的东西,请随时提出建议.

谢谢你的帮助

最佳答案 尝试在XSD2Code参数中将ChoiceIdentifier值设置为’array’而不是’list’.

点赞