简体   繁体   English

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

[英]Error deserializing XML in C#

Here is my code for deserialization 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);
    }

I've generated classes from link http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd using Visual Studio's tool xsd.exe. 我使用Visual Studio的工具xsd.exe从链接http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd生成了类。 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 , and they are compatible). 我有一些基于LandXML Schema的通用文件(XSD和XML文件在http://www.utilities-online.info/xsdvalidation/#.VtBcNeaT6YA上检查兼容性,它们是兼容的)。 The thing is that my code never get further than: 问题是我的代码永远不会超越:

XmlSerializer mySerializer =
    new XmlSerializer(typeof(LandXML));

and I get an error (this is only a part of error trace) 我收到一个错误(这只是错误跟踪的一部分)

'TunnelCore.Utilities.LandXML12.LandXML'. 'TunnelCore.Utilities.LandXML12.LandXML'。 System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.LandXML'. System.InvalidOperationException:存在反映类型“TunnelCore.Utilities.LandXML12.LandXML”的错误。 ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException:反映属性'Items'时出错。 ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeatures'. ---> System.InvalidOperationException:有一个错误反映了类型'TunnelCore.Utilities.LandXML12.PlanFeatures'。 ---> System.InvalidOperationException: There was an error reflecting property 'PlanFeature'. ---> System.InvalidOperationException:反映属性'PlanFeature'时出错。 ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.PlanFeature'. ---> System.InvalidOperationException:有一个错误反映了类型'TunnelCore.Utilities.LandXML12.PlanFeature'。 ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException:反映属性'Items'时出错。 ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.CoordGeom'. ---> System.InvalidOperationException:有一个错误反映了类型'TunnelCore.Utilities.LandXML12.CoordGeom'。 ---> System.InvalidOperationException: There was an error reflecting property 'Items'. ---> System.InvalidOperationException:反映属性'Items'时出错。 ---> System.InvalidOperationException: There was an error reflecting type 'TunnelCore.Utilities.LandXML12.IrregularLine'. ---> System.InvalidOperationException:有一个错误反映了类型'TunnelCore.Utilities.LandXML12.IrregularLine'。 ---> System.InvalidOperationException: There was an error reflecting property 'Item'. ---> System.InvalidOperationException:反映属性'Item'时出错。 ---> System.InvalidOperationException: The type for XmlElement may not be specified for primitive types. ---> 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) --- 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.XmlReflectionImport 在System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping访问器,FieldModel模型,XmlAttributes a,String ns,Type choiceIdentifierType,Boolean rpc,Boolean openModel,RecursionLimiter limiter)处于System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent,FieldModel) System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping,StructModel model,Boolean openModel,String typeName,RecursionLimiter limiter)中的model,XmlAttributes a,String ns,RecursionLimiter limiter)---内部异常堆栈跟踪结束--- at System.Xml.Serialization.XmlReflectionImporter.ImportStructLikeMapping的System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping,StructModel model,Boolean openModel,String typeName,RecursionLimiter limiter)(StructModel model,String ns,Boolean openModel,XmlAttributes a,RecursionLimiter limiter) )在System.Xml.Serialization.XmlReflectionImport er.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) er.ImportTypeMapping(TypeModel model,String ns,ImportContext context,String dataType,XmlAttributes a,Boolean repeats,Boolean openModel,RecursionLimiter limiter)

I'm new at programming, but I think that this part is maybe crucial: 我是编程新手,但我认为这部分可能至关重要:

...System.InvalidOperationException: The type for XmlElement may not be specified for primitive types. ... System.InvalidOperationException:可能未为基本类型指定XmlElement的类型。

Can someone, please, help to parse and create this classes correctly. 有人可以帮助解析并正确创建这个类。 Sample XML files for testing can be found at: 用于测试的示例XML文件可在以下位置找到:

http://landxml.org/schema/LandXML-1.1/samples/TopoCAD/Alignments%20and%20length%20table.xml http://landxml.org/schema/LandXML-1.1/samples/TopoCAD/Alignments%20and%20length%20table.xml

Following your steps I created the classes and compared the irregular line class to the Boundary class since both have a choice that has only 2 items PntList2d/3d. 按照您的步骤创建类并将不规则的线类与Boundary类进行比较,因为两者都有一个只有2个项PntList2d / 3d的选项。

For Irregular Line 对于不规则线

    [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;
        }
    }

And For Boundary 而对于边界

    [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;
        }
    }

I believe your issue is Irregular lines Item field is a double and The return type for Item is also a double. 我相信你的问题是不规则的行项目字段是双重的,而Item的返回类型也是双重的。

Changing both to object will get rid of the error 将两者都更改为object将消除错误

BUT you have more errors... 但是你有更多错误......

Notice that boundary doesn't have 请注意,边界没有

[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]

It also doesnt have 它也没有

private ItemChoiceType itemElementNameField;

/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemChoiceType ItemElementName {
    get {
        return this.itemElementNameField;
    }
    set {
        this.itemElementNameField = value;
    }
}

You will need to add these as well in a few different areas (it will tell you where). 您还需要在几个不同的区域添加这些(它会告诉您在哪里)。 You were correct about the error being crucial. 关于错误是至关重要的,你是对的。 Each place you need to make changes will be at the bottom of those nested inner exceptions. 您需要进行更改的每个位置都位于这些嵌套内部异常的底部。 Keep working at it, there are probably several errors that will only appear once you get rid of the error before it. 继续工作,可能会出现一些错误,只有在你摆脱错误之前才会出现这些错误。 Apparently xsd.exe makes a few mistakes with choices 显然xsd.exe在选择上犯了一些错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM