67865

C# How to fill class created using XSD.EXE from database

Question:

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.

<strong>EDIT</strong>

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

Answer1:

If you get your XML from the database as a string you can use a <a href="http://msdn.microsoft.com/en-us/library/system.io.stringreader.aspx" rel="nofollow">StringReader</a>. Or if you get a byte[] you may try a <a href="http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx" rel="nofollow">MemoryStream</a>.

[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.<br /><strong><edit> Added xml.Items[0] = new YourItemsType();</edit></strong>

[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; }

Answer2:

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

Recommend

  • Can not Resolved FirebaseOptions.Builder().setCredentials()
  • Is changing parent process necessary when daemonize a process?
  • how to perform a modal segue for long press
  • A floating button fixed at the bottom of UITableview with scrollViewDidScroll not working properly
  • PHP how to have code continue without waiting for results of include
  • CSS borders style INSET or GROOVE look very different from IE9 FF4 or Safari5 or Chrome2
  • Forcing RTL order in a JTextArea
  • Hibernate artifact id “hibernate-core” with version 4.3.5.Final and artifact id “hibernate” with ver
  • Regular Expression Using the Dot-Matches-All Mode
  • How enable GZIP compression for Spring HttpInvoker requests using Tomcat?
  • Json.net Add property to every class containing of a certain type
  • Is sparse BLAS not included in BLAS?
  • Using NinePatch (.9.png) for making scaled Bitmaps
  • Vim and ASCII extended characters?
  • Gson serializing Spring beans
  • Can someone please explain how this implementation of bucket sort works?
  • Unity 5.1 Animator Controller not transitioning
  • How to change default stop edit behavior in jtable
  • How to replace TouchesBegan with UIGestureRecognizer
  • Specify HTTPS for custom WCF Binding
  • What's the name of this finding square root algorithm?
  • Telegram bot API - Inline bot getting Error 400 while trying to answer inline query
  • Making Google Visualization - Annotation Chart to work in GWT
  • List comprehension with if conditional to get list of files of a specific type
  • Getting short path in python
  • Best way to dynamically load an xml configuration file into a Flex4/Flash movie at runtime?
  • Cast between interfaces whose interface signatures are same
  • How can I set a binding to a Combox in a UserControl?
  • JQuery Internet Explorer and ajaxstop
  • Use of this Javascript
  • C++ Partial template specialization - design simplification
  • How to delay loading a property with linq to sql external mapping?
  • Cannot connect to cassandra from Spark
  • QLineEdit password safety
  • How to make Safari send if-modified-since header?
  • How to get next/previous record number?
  • How to pass list parameters for each object using Spring MVC?
  • Python: how to group similar lists together in a list of lists?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • JaxB to read class hierarchy