在 C# 中反序列化 XML 时出错
本文关键字:出错 XML 反序列化 | 更新日期: 2023-09-27 18:36:28
这是我在 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);
}
我使用Visual Studio的工具xsd.exe从链接 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd 生成类。我有一些基于 LandXML 架构的通用文件(XSD 和该 XML 文件在 http://www.utilities-online.info/xsdvalidation/#.VtBcNeaT6YA 时检查兼容性,并且它们是兼容的)。问题是我的代码永远不会比:
XmlSerializer mySerializer =
new XmlSerializer(typeof(LandXML));
我收到一个错误(这只是错误跟踪的一部分)
'TunnelCore.Utilities.LandXML12.LandXML'. System.InvalidOperationException:存在反映类型"TunnelCore.Utilities.LandXML12.LandXML"的错误。---> System.InvalidOperationException:反映属性"Items"时出错。---> System.InvalidOperationException:存在反映类型"TunnelCore.Utilities.LandXML12.PlanFeatures"的错误。---> System.InvalidOperationException:反映属性"PlanFeature"时出错。---> System.InvalidOperationException:存在反映类型"TunnelCore.Utilities.LandXML12.PlanFeature"的错误。---> System.InvalidOperationException:反映属性"Items"时出错。---> System.InvalidOperationException:存在反映类型"TunnelCore.Utilities.LandXML12.CoordGeom"的错误。---> System.InvalidOperationException:反映属性"Items"时出错。---> System.InvalidOperationException:存在反映类型"TunnelCore.Utilities.LandXML12.IrregularLine"的错误。---> System.InvalidOperationException: 反映属性"Item"时出错。---> System.InvalidOperationException:不能为基元类型指定 XmlElement 的类型。 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) ---内部异常堆栈跟踪结束--- 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:不能为基元类型指定 XmlElement 的类型。
请有人帮助正确解析和创建此类。用于测试的示例 XML 文件可在以下位置找到:
http://landxml.org/schema/LandXML-1.1/samples/TopoCAD/Alignments%20and%20length%20table.xml
按照您的步骤,我创建了类并将不规则线类与边界类进行了比较,因为两者都可以选择只有 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 的返回类型也是双精度值。
将两者更改为对象将消除错误
但是你有更多的错误...
请注意,边界没有
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
它也没有
private ItemChoiceType itemElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
get {
return this.itemElementNameField;
}
set {
this.itemElementNameField = value;
}
}
您还需要在几个不同的区域添加这些内容(它会告诉您在哪里)。你对错误至关重要的看法是正确的。 您需要进行更改的每个位置都将位于这些嵌套的内部异常的底部。 继续努力,可能有几个错误只有在您摆脱错误之前才会出现。 显然 xsd.exe 在选择上犯了一些错误