Normally I write a class and add XML Serialization to it for my web services.
[XmlRootAttribute(ElementName = "dsXmlSummary", IsNullable=true)]
public class Class1
{
//declare properties
//make database calls to pull data and load properties
}
I am working on a project that requires me to use a strict XSD, I've followed instructions on using the XSD.EXE tool to create a class based on the XSD. My interpretation was that this auto-generated class would replace my normal serialized class.
If this is the case I am completely lost on loading the data into the class properties. I've gathered this from another walk through:
[WebMethod]
public dsXmlSummary getXML()
{
TextReader reader = new StreamReader("data.xml");
dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader);
reader.Close();
}
However the data I have is located in a SQL database...I figured I should be able to write a method to fill the dsXmlSummary
class, however I cannot find any documentation on doing this. All examples are like the above, loading or reading from an actual physical xml document.
I tried testing out a manual load:
[WebMethod]
public dsXmlSummary getXML()
{
dsXmlSummary xml = new dsXmlSummary();
xml.Items[0].nameFirst = "Mark"; //error thrown here: System.NullReferenceException: Object reference not set to an instance of an object.
xml.Items[0].nameLast = "Twain";
xml.Items[0].Type = "Writer";
return xml;
}
Obviously I'm going about this all wrong. Any guidance is greatly appreciated.
EDIT
My WebMethod
[WebMethod]
public dsXmlSummary getXML()
{
dsXmlSummary xml = new dsXmlSummary();
dsXmlSummaryAdmin_reports_xmlReports[] items = new dsXmlSummaryAdmin_reports_xmlReports[1];
items[0].nameFirst = "Mark"; //error still thrown here: System.NullReferenceException: Object reference not set to an instance of an object.
items[0].nameLast = "Twain";
items[0].Type = "Writer";
xml.Items = items;
return xml;
}
The Auto generated class
public partial class dsXmlSummary {
private dsXmlSummaryAdmin_reports_xmlReports[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("admin_reports_xmlReports")]
public dsXmlSummaryAdmin_reports_xmlReports[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}