gpt4 book ai didi

c# - 在 C# 中反序列化 XML 时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:20:19 26 4
gpt4 key购买 nike

这是我在 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);
}

我从链接 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd 生成了类使用 Visual Studio 的工具 xsd.exe。我有一些基于 LandXML Schema 的通用文件(XSD 和该 XML 文件在 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

最佳答案

按照您的步骤,我创建了类并将不规则线类与边界类进行了比较,因为两者都有一个只有 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 字段是 double 的并且 Item 的返回类型也是 double 的。

将两者都更改为 object 将消除错误

但是你有更多的错误...

注意边界没有

[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# - 在 C# 中反序列化 XML 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658403/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com