I was following some tutorials on how to use xsd.exe to deserialize XML. I got two generated files and everything seemed to work fine.
But then I noticed that I can't get to the itemsField with, like it should be in my example, room.Items[0].
After a bit of searching, it turned out it is deserializing to a DataSet and I have to iterate with computers.element.Rows. I don't want to do that and I don't know why I got the DataSet option. In my XSD I do have msdata:IsDataSet="true" option but changing it to false or/and generating xsd room.xsd with /classes switch doesn't work.
What could I be missing here?
Side note: When I referenced both room.cs and room.xsd to my project at the same time, it got blown to 4 different files and built correctly. I do get the Message 1 Could not find schema information for the element 'room'.
coming from Room.xml when my build has errors but when I iterate through this DataSet I don't want, I get all of my nodes correctly.
Anything except room.cs is a lot of automated garbage I don't really understand (for example the room.Designer class which is long and awful), so here is my 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;
}
}
}
and XSD generated with xsd room.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>
And as for the code executing the XmlSerializer, it's plain old simple:
public static room xmlHandler(StreamReader xmlFile)
{
var serializer = new XmlSerializer(typeof(room));
return (room)serializer.Deserialize(xmlFile);
}
If anything else is needed, I'll post it! I don't know what I am doing wrong, to be honest. Thought it was going to be super easy, now I'm stuck.
Thank you for all the help!
TLDR: Can't make my app use room.Items[] instead of room.device.Rows after deserialization. Files generated with xsd.exe.