我正在關注如何使用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