views:

133

answers:

2

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;
    }
  }
}
A: 

As far as I remember, you can get the Dataset/Datatable XML (ToString()? or may be WriteXML)_, not sure).. then use that

Shady M. Najib
Sorry, my question explanation was unclear. I edited it, hopefully it makes a little more sense now. Thanks for your response though.
jon3laze
Aha.. sorry, I kinda skimmed your question.. anyway, I guess Albin's answer is sufficient now
Shady M. Najib
+3  A: 

If you get your XML from the database as a string you can use a StringReader. Or if you get a byte[] you may try a MemoryStream.

[WebMethod]
public dsXmlSummary getXML()
{
    TextReader reader = new StringReader("<dsXmlSummary><FirstName>Mark</FirstName></dsXmlSummary>");
    dsXmlSummary ds = (dsXmlSummary)serializer.Deserialize(reader);
    reader.Close();
}

Regarding your "manual" example you need to initialize your Items array.
<edit> Added xml.Items[0] = new YourItemsType();</edit>

[WebMethod]
public dsXmlSummary getXML()
{
    dsXmlSummary xml = new dsXmlSummary();
    xml.Items = new YourItemsType[1];    // <-- initialize here
    xml.Items[0] = new YourItemsType();  // <-- initialize first object
    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;
}
Albin Sunnanbo
I guess that's my problem. I know how to get from stream to serialization...However the data is not structured as XML in the database. I wrote a class that pulls the data from the database and then made the class itself serializable setting the xml properties...i'm just not sure how to get that from a class to a stream.
jon3laze
@jon3laze: Oops, forgot to initialize the first element too, corrected my example, try again.
Albin Sunnanbo