使用 xsd 反序列化.exe - 如何反序列化为对象,而不是 DataSet

本文关键字:反序列化 DataSet 对象 exe xsd 使用 | 更新日期: 2023-09-27 18:33:00

我正在学习一些关于如何使用xsd.exe来反序列化XML的教程。我得到了两个生成的文件,一切似乎都很好。

但后来我注意到我无法进入项目字段,就像它应该在我的例子中一样,房间。物品[0]。

经过一番搜索,结果发现它正在反序列化为数据集,我必须使用 computers.element.Rows 进行迭代。我不想这样做,我不知道为什么我得到了数据集选项。在我的XSD中,我确实有msdata:IsDataSet="true"选项,但将其更改为false或/并使用/classes开关生成xsd room.xsd不起作用。

我在这里可能缺少什么?旁注:当我同时将 room.cs 和 room.xsd 引用到我的项目中时,它被吹成了 4 个不同的文件并正确构建。当我的构建有错误时.xml我确实得到了来自 Room 的Message 1 Could not find schema information for the element 'room'.,但是当我循环访问我不想要的这个数据集时,我正确地获得了所有节点。

除了房间.cs之外的任何东西都是很多我不真正理解的自动垃圾(例如房间。设计师课又长又糟糕(,所以这是我的房间.cs:

using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[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 room {
private roomDevice[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("device", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public roomDevice[] Items {
    get {
        return this.itemsField;
    }
    set {
        this.itemsField = value;
    }
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class roomDevice {
private string xcoordField;
private string ycoordField;
private string macaddressField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string xcoord {
    get {
        return this.xcoordField;
    }
    set {
        this.xcoordField = value;
    }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ycoord {
    get {
        return this.ycoordField;
    }
    set {
        this.ycoordField = value;
    }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string macaddress {
    get {
        return this.macaddressField;
    }
    set {
        this.macaddressField = value;
    }
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type {
    get {
        return this.typeField;
    }
    set {
        this.typeField = value;
    }
}
}

以及使用 XSD 房间生成的 XSD.xml:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="room" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema"  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="room" msdata:IsDataSet="true" msdata:Locale="en-US">
<xs:complexType>
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="device">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="xcoord" type="xs:string" minOccurs="0" msdata:Ordinal="0" />
          <xs:element name="ycoord" type="xs:string" minOccurs="0" msdata:Ordinal="1" />
          <xs:element name="macaddress" type="xs:string" minOccurs="0" msdata:Ordinal="2" />
        </xs:sequence>
        <xs:attribute name="type" type="xs:string" />
      </xs:complexType>
    </xs:element>
  </xs:choice>
</xs:complexType>

至于执行 XmlSerializer 的代码,它很简单:

public static room xmlHandler(StreamReader xmlFile)
    {
        var serializer = new XmlSerializer(typeof(room));
        return (room)serializer.Deserialize(xmlFile);
    }

如果还需要什么,我会发布它!老实说,我不知道我做错了什么。以为这会非常容易,现在我卡住了。

谢谢大家的帮助!

TLDR:无法使我的应用程序占用空间。反序列化后的 items[] 而不是 room.device.row。使用 xsd.exe 生成的文件。

使用 xsd 反序列化.exe - 如何反序列化为对象,而不是 DataSet

终于我

让它工作了!如果有人再次遇到这个问题,这很简单。我已经生成了我的两个文件,使用:

xsd room.xml
xsd room.xsd /classes

它生成了room.xsd和room.cs。正如我之前所说,当我将它们添加到我的项目中时,它被吹成了 4 个文件,其中一个文件导致了问题。

问题是房间。设计器文件。它指定聊天室继承自数据集和两个聊天室。设计器和房间.cs被标记为分部类。我删除了cs。设计器并删除了部分前缀并使其工作。我想这是Visual Studio内置的设计器的问题,可以通过正确的设置完成不那么粗糙,但结果也很棒。