C# 尝试反序列化XML文件

C# 尝试反序列化XML文件,c#,xml,serialization,xsd,C#,Xml,Serialization,Xsd,我使用xsd.exe生成了以下类: using System.Xml.Serialization; [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")] [System.SerializableAttribute()] [System.Diagnostics.DebuggerStepThroughAttribute()] [System.ComponentModel.DesignerCategoryAtt

我使用xsd.exe生成了以下类:

using System.Xml.Serialization;

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class ROOT {

private PersonBaseType pERSONField;

/// <remarks/>
public PersonBaseType PERSON {
    get {
        return this.pERSONField;
    }
    set {
        this.pERSONField = value;
    }
}
}

[System.Xml.Serialization.XmlIncludeAttribute(typeof(NotWorkerPersonType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(WorkerPersonType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class PersonBaseType {

private MotorVehicleType vEHICLEField;

public MotorVehicleType VEHICLE {
    get {
        return this.vEHICLEField;
    }
    set {
        this.vEHICLEField = value;
    }
}
}

[System.Xml.Serialization.XmlIncludeAttribute(typeof(CarVehicleType))]
[System.Xml.Serialization.XmlIncludeAttribute(typeof(AutobusVehicleType))]
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public abstract partial class MotorVehicleType {

private string nameField;

public string Name {
    get {
        return this.nameField;
    }
    set {
        this.nameField = value;
    }
}
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class CarVehicleType : MotorVehicleType {
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class AutobusVehicleType : MotorVehicleType {
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class NotWorkerPersonType : PersonBaseType {   
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class WorkerPersonType : PersonBaseType {
}
我知道我应该指定车辆的类型,但是为什么xml通过了模式验证?如果Person元素为WorkerPersonType,则车辆只能使用AutobusVehicleType。这就是我在xsd模式中指定person的方式

我应该换点我班上的东西吗?我应该添加一些属性还是其他什么


我不知道如何修复此错误

三条建议:

<?xml version="1.0" encoding="UTF-8"?>
<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="file:///C:/Users/SIGFRID/Documents/Visual%20Studio%202012/Projects/sample/ConsoleApplication1/ConsoleApplication1/MySample.xsd">
  <PERSON xsi:type="WorkerPersonType">
    <VEHICLE>
      <Name>VOLVO</Name>
    </VEHICLE>
  </PERSON>
</ROOT>
  • 使用
    xsi
    作为
    http://www.w3.org/2001/XMLSchema-instance
    。这不是必需的 但这是标准
  • 对路径中带有空格的本地XSD使用以下语法:
    file:///C:/Users/SIGFRID/Documents/Visual%20Studio%202012/Projects/sample/ConsoleApplication1/ConsoleApplication1/MySample.xsd
  • 使用
    xsi:noNamespaceSchemaLocation
    而不是
    xsi:schemaLocation
    因为您没有使用名称空间
  • 这是您的XML文件,其中包含应用的建议:

    <?xml version="1.0" encoding="UTF-8"?>
    <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="file:///C:/Users/SIGFRID/Documents/Visual%20Studio%202012/Projects/sample/ConsoleApplication1/ConsoleApplication1/MySample.xsd">
      <PERSON xsi:type="WorkerPersonType">
        <VEHICLE>
          <Name>VOLVO</Name>
        </VEHICLE>
      </PERSON>
    </ROOT>
    
    
    沃尔沃汽车
    
    谢谢您的建议。不幸的是,它仍然不起作用。我认为我用xsd.exe工具生成的类是不正确的。我读到xsd.exe工具有一些限制,并不总是生成正确的类。我修复了上面xsd路径规范中的一些错误。另外,我刚刚注意到在你的问题中有一条完整的错误信息——由于未格式化而丢失。在问题中编辑/固定。我注意到您在XML中为PERSON指定了非抽象类型,但没有为VEHICLE指定非抽象类型,XSD将VEHICLE定义为抽象类型。这个错误表明必须解决这种不一致性。是的,我也这么认为。但是XSD中WorkerPersonType的定义间接地包含了允许的载体类型。我认为这样可能应该更改生成的类中的任何内容,以便能够反序列化XML。但是如果是不一致的,为什么xml验证会通过?(1)我不愿意手动更改生成代码中的某些内容。(2) 我倾向于相信XML验证,特别是基于Xerces工具的XML验证是正确的。也许您遇到了xsd.exe的限制。您能否在不依赖xsd.exe正确处理@xsi:type驱动的抽象类(
    PERSON
    )差异和相关的子类(
    VEHICLE
    )隐含实例化的情况下完成任务?
    The specified type is abstract: name='MotorVehicleType', Namespace='', bei <VEHICLE xmlns=''>."}
    
    <?xml version="1.0" encoding="UTF-8"?>
    <ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="file:///C:/Users/SIGFRID/Documents/Visual%20Studio%202012/Projects/sample/ConsoleApplication1/ConsoleApplication1/MySample.xsd">
      <PERSON xsi:type="WorkerPersonType">
        <VEHICLE>
          <Name>VOLVO</Name>
        </VEHICLE>
      </PERSON>
    </ROOT>