我正在关注如何使用xsd.exe来反序列化XML的一些教程。我有两个生成的文件,一切似乎都很好。使用xsd.exe反序列化 - 如何反序列化为对象,而不是DataSet?
但后来我注意到我无法进入itemsField,就像它应该在我的例子中,room.Items [0]。
经过一番搜索后,事实证明它是反序列化到一个DataSet,我必须迭代computers.element.Rows。我不想这样做,我不知道为什么我有DataSet选项。在我的XSD中,我确实有msdata:IsDataSet =“true”选项,但将其更改为false或/并使用/ classes开关生成xsd room.xsd不起作用。
什么可能我会丢失吗? 备注:当我同时将room.cs和room.xsd同时引用到我的项目时,它会被烧制到4个不同的文件并正确构建。当我的构建出现错误时,我确实得到了来自Room.xml的Message 1 Could not find schema information for the element 'room'.
,但是当我遍历这个不需要的DataSet时,我正确地获得了所有节点。
任何东西,除了room.cs是很多自动化的垃圾我真的不明白(例如room.Designer类,这是漫长而可怕的),所以这里是我的room.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 room.xml
和XSD:
<?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:反序列化后无法让我的应用程序使用room.Items []而不是room.device.Rows。使用xsd.exe生成的文件。
这响铃,让我们知道如果这脱光了问题,因为如果您按照我的指示在这里不需要DataSet:http://stackoverflow.com/a/12233006/495455 – 2014-12-03 14:00:20