2

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.

wtrmeln
  • 121
  • 11
  • This rings a bell, let us know if this sheds light on the problem because a DataSet isn't needed if you follow my instructions here: http://stackoverflow.com/a/12233006/495455 – Jeremy Thompson Dec 03 '14 at 14:00

1 Answers1

0

Finally I got it to work! If someone ever runs into this problem again, it is very simple. I've generated my two files, using:

xsd room.xml
xsd room.xsd /classes

It generated room.xsd and room.cs. When I added them to my project, as I said before, it got blown to 4 files, one of them causing problems.

The problem is the room.Designer file. It specifies that the room is inheriting from DataSet and both room.Designer and room.cs are marked as a partial class. I deleted the cs.Designer and deleted the partial prefix and got it to work. I guess it is a problem with the Designer built into Visual Studio and it can be done less rough with correct settings but this also turned out great.

wtrmeln
  • 121
  • 11