在 C# 中反序列化 XML 时出错
Error deserializing XML in C#
这是我在 C# 中的反序列化代码:
private void button1_Click(object sender, EventArgs e)
{
LandXML myObject;
XmlSerializer mySerializer =
new XmlSerializer(typeof(LandXML));
FileStream myFileStream =
new FileStream("Nova 6.xml", FileMode.Open);
myObject = (LandXML)
mySerializer.Deserialize(myFileStream);
}
我从 link http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd using Visual Studio's tool xsd.exe. I have some generic file based on LandXML Schema (XSD and that XML file are checked for compatibility at http://www.utilities-online.info/xsdvalidation/#.VtBcNeaT6YA 生成了 类,它们是兼容的)。问题是我的代码永远不会超过:
XmlSerializer mySerializer =
new XmlSerializer(typeof(LandXML));
我得到一个错误(这只是错误跟踪的一部分)
'TunnelCore.Utilities.LandXML12.LandXML'.
System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.LandXML'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeatures'. ---> System.InvalidOperationException: There was an error reflecting property 'PlanFeature'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeature'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.CoordGeom'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.IrregularLine'. ---> System.InvalidOperationException: There was an error reflecting property 'Item'. ---> System.InvalidOperationException: The type for XmlElement may not be specified for primitive types.
at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
--- End of inner exception stack trace ---
at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter)
at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
我是编程新手,但我认为这部分可能很重要:
...System.InvalidOperationException: The type for XmlElement may not be specified for primitive types.
有人可以帮助正确解析和创建这个 类 吗?可在以下位置找到用于测试的示例 XML 文件:
http://landxml.org/schema/LandXML-1.1/samples/TopoCAD/Alignments%20and%20length%20table.xml
按照您的步骤,我创建了 classes 并将不规则线 class 与边界 class 进行了比较,因为两者都有一个只有 2 个项目的选择 PntList2d/3d .
不规则线
[System.Xml.Serialization.XmlElementAttribute("PntList2D", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("PntList3D", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public double Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
和边界
[System.Xml.Serialization.XmlElementAttribute("PntList2D", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("PntList3D", typeof(string))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
我认为您的问题是不规则行 Item 字段是双精度并且 Item 的 return 类型也是双精度。
将两者都更改为对象将消除错误
但是你有更多的错误...
注意边界没有
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
它也没有
private ItemChoiceType itemElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
您还需要在几个不同的区域添加这些(它会告诉您在哪里)。
您关于错误至关重要的说法是正确的。您需要进行更改的每个地方都将位于那些嵌套内部异常的底部。继续努力,可能有几个错误只会在您摆脱之前的错误后才会出现。 Apparently xsd.exe makes a few mistakes with choices
这是我在 C# 中的反序列化代码:
private void button1_Click(object sender, EventArgs e)
{
LandXML myObject;
XmlSerializer mySerializer =
new XmlSerializer(typeof(LandXML));
FileStream myFileStream =
new FileStream("Nova 6.xml", FileMode.Open);
myObject = (LandXML)
mySerializer.Deserialize(myFileStream);
}
我从 link http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd using Visual Studio's tool xsd.exe. I have some generic file based on LandXML Schema (XSD and that XML file are checked for compatibility at http://www.utilities-online.info/xsdvalidation/#.VtBcNeaT6YA 生成了 类,它们是兼容的)。问题是我的代码永远不会超过:
XmlSerializer mySerializer =
new XmlSerializer(typeof(LandXML));
我得到一个错误(这只是错误跟踪的一部分)
'TunnelCore.Utilities.LandXML12.LandXML'. System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.LandXML'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeatures'. ---> System.InvalidOperationException: There was an error reflecting property 'PlanFeature'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeature'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.CoordGeom'. ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.IrregularLine'. ---> System.InvalidOperationException: There was an error reflecting property 'Item'. ---> System.InvalidOperationException: The type for XmlElement may not be specified for primitive types. at System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) --- End of inner exception stack trace --- at System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping(StructModel model, String ns, Boolean openModel, XmlAttributes a, RecursionLimiter limiter) at System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter)
我是编程新手,但我认为这部分可能很重要:
...System.InvalidOperationException: The type for XmlElement may not be specified for primitive types.
有人可以帮助正确解析和创建这个 类 吗?可在以下位置找到用于测试的示例 XML 文件:
http://landxml.org/schema/LandXML-1.1/samples/TopoCAD/Alignments%20and%20length%20table.xml
按照您的步骤,我创建了 classes 并将不规则线 class 与边界 class 进行了比较,因为两者都有一个只有 2 个项目的选择 PntList2d/3d .
不规则线
[System.Xml.Serialization.XmlElementAttribute("PntList2D", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("PntList3D", typeof(string))]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
public double Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
和边界
[System.Xml.Serialization.XmlElementAttribute("PntList2D", typeof(string))]
[System.Xml.Serialization.XmlElementAttribute("PntList3D", typeof(string))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
我认为您的问题是不规则行 Item 字段是双精度并且 Item 的 return 类型也是双精度。
将两者都更改为对象将消除错误
但是你有更多的错误...
注意边界没有
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
它也没有
private ItemChoiceType itemElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
您还需要在几个不同的区域添加这些(它会告诉您在哪里)。 您关于错误至关重要的说法是正确的。您需要进行更改的每个地方都将位于那些嵌套内部异常的底部。继续努力,可能有几个错误只会在您摆脱之前的错误后才会出现。 Apparently xsd.exe makes a few mistakes with choices