XML 文档 (2,2) 中的反序列化错误

Deserialization error in XML document(2,2)

我正在尝试反序列化通过 xmlvalidate 验证的 xml 文档。我已将文件缩减到最低限度,但出现上述错误。

        string tteFilepath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        tteFilepath += "\" + "SkeletonNetwork.xml";

        XmlSerializer serializer = new XmlSerializer(typeof(NetworkDescriptionDTO));
        NetworkDescription networkDescription = new NetworkDescription();
        NetworkDescriptionDTO ndDTO = new NetworkDescriptionDTO();

        try
        {

            FileStream fs = new FileStream(tteFilepath, FileMode.Open);
            ndDTO = (NetworkDescriptionDTO)serializer.Deserialize(fs);
            fs.Close();

        }
        catch (Exception parseEx)
        {

            String exception = parseEx.Message;

        }

裸XML如下:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<nd:NetworkDescription
    xmi:version="2.0"
    xmlns:xmi="http://www.omg.org/XMI"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:buf="http://www.tttech.com/Schema/TTEthernet/Network_Description/Buffering/5.4.0"
    xmlns:c="http://www.tttech.com/Schema/TTEthernet/Network_Description/Constraint/5.4.0"
    xmlns:flows="http://www.tttech.com/Schema/TTEthernet/Network_Description/Flows/5.4.0"
    xmlns:logical="http://www.tttech.com/Schema/TTEthernet/Network_Description/Logical/5.4.0"
    xmlns:nd="http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0"
    xmlns:sync="http://www.tttech.com/Schema/TTEthernet/Network_Description/Synchronization/5.4.0"
    xmlns:t="http://www.tttech.com/Schema/TTEthernet/Network_Description/Triggers/5.4.0"
    xmlns:topo="http://www.tttech.com/Schema/TTEthernet/Network_Description/Topology/5.4.0"
    name="tte_demo"
    enableDynamicRouting="true"
    createUnknownDefaultRoutes="true"
    ctMarker="//@flowMarker[name='ctMarker']">
</nd:NetworkDescription>

我尝试在 ns 路径末尾保留版本,以及对 xml 文件进行的其他调整和摆弄,但没有任何效果。

错误是:

"There is an error in XML document (2, 2)."

InnerException 是:

"http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0'> was not expected."

NetworkDescriptionDTO 的 class 是使用 xsd.exe 工具使用适当的 xsd 文件创建的。

有人有什么想法吗?

下面是 class 的片段。

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0", IsNullable = false)]
    public partial class NetworkDescriptionDTO : NamedElementDTO
    {

        private object[] items2Field;

        private bool enableDynamicRoutingField;

        private bool createUnknownDefaultRoutesField;

        private string ctMarkerField;

        public NetworkDescriptionDTO()
        {
            this.enableDynamicRoutingField = false;
            this.createUnknownDefaultRoutesField = false;
        }

问题似乎出在 XMLRootAttribute 的 ElementName 参数上。我的理论是 class 名称和 xml 节点名称不同导致了这个问题。将 ElementName 添加到 XMLRootAttribute 使序列化无一例外地完成。

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.0")]
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0")]
    [System.Xml.Serialization.XmlRootAttribute(ElementName = "NetworkDescription", Namespace = "http://www.tttech.com/Schema/TTEthernet/Network_Description/5.4.0", IsNullable = false)]
    public partial class NetworkDescriptionDTO : NamedElementDTO
    {

        private object[] items2Field;

        private bool enableDynamicRoutingField;

        private bool createUnknownDefaultRoutesField;

        private string ctMarkerField;

        public NetworkDescriptionDTO()
        {
            this.enableDynamicRoutingField = false;
            this.createUnknownDefaultRoutesField = false;
        }