How to use XSD Generated C# class.

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<div style="margin:1em 2em 0px 4.5em; padding:0px; border:0px; font-size:14px; vertical-align:baseline; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px
<div style="margin:0px 0px 2em; padding:0px; border:0px; font-size:1em; vertical-align:baseline; clear:right
<p style="vertical-align:baseline; list-style-type:none <span style="font-size:1em My data is being passed to a class not coming from a database. So Im going to use the generated class from XSD to populate the XML and then serialize it.<br/>

<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none Heres the XML sample. The Schema, and then the generated class. My code is FIRST - my question is how to get this "hdr" item - the headers - into "items". Thats my C# question. As
an aside, serialization fails because the generated class has a [] [] item in it that should be [] - I dont know why XSD does this. See my comments in the generated class. Look for TAH - where you see [][], Id also like a comment on that.
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none Heres my C#
<p style="vertical-align:baseline; list-style-type:none <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline TAXPYMTHeader<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline hdr
= <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline new<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline TAXPYMTHeader<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline ();
<p style="vertical-align:baseline; list-style-type:none tp = <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline null;
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline Item itx = <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline new <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline Item();
<p style="vertical-align:baseline; list-style-type:none itx.Name = <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline "CASHIER_INIT";
<p style="vertical-align:baseline; list-style-type:none itx.Value = <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline "TAH";
<p style="vertical-align:baseline; list-style-type:none <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline Item[] arr3 = <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline new <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline Item[]
{ itx };
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none hdr.Item = arr3;
<p style="vertical-align:baseline; list-style-type:none // in need to get this "hdr" as one of the items in tp.items
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none HOW?
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline XmlSerializer<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline serx
= <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline new<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline XmlSerializer<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline (<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline typeof<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline (TAXPYMT<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline ));
<p style="vertical-align:baseline; list-style-type:none <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline using (<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline TextWriter w1
= <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline new <span style="margin:0px; padding:0px; border:0px; vertical-align:baseline StreamWriter(<span style="margin:0px; padding:0px; border:0px; vertical-align:baseline "testing.xml"))
<p style="vertical-align:baseline; list-style-type:none {
<p style="vertical-align:baseline; list-style-type:none serx.Serialize(w1, tp);
<p style="vertical-align:baseline; list-style-type:none w1.Close();
<p style="vertical-align:baseline; list-style-type:none }
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none The XML the XSD generated from:
<p style="vertical-align:baseline; list-style-type:none

<p style="vertical-align:baseline; list-style-type:none <?xml version="1.0" encoding="utf-8"?><br/>
<TAXPYMT><br/>
<!-- There will be only one of these. --><br/>
<Header><br/>
<Item Name="REGISTER_NO" Value=""/><br/>
<Item Name="CASHIER_INIT" Value=""/><br/>
<Date Name="EFFECTIVE_DATE" Year="" Month="" Day="" Hour="" Minute="" Second="" Millisecond=""/><br/>
<Date Name="BUSINESS_DATE" Year="" Month="" Day="" Hour="" Minute="" Second="" Millisecond=""/><br/>
<Date Name="ACTUAL_DATE" Year="" Month="" Day="" Hour="" Minute="" Second="" Millisecond=""/><br/>
</Header><br/>
<br/>
<PaymentList><br/>
<!-- There will be one of these for each receipt number being paid. --><br/>
<Payment><br/>
<Item Name="RECEIPT_NO" Value=""/><br/>
<Item Name="PARID" Value=""/><br/>
<Item Name="TAXYEAR" Value=""/><br/>
<Item Name="PAYMENT_AMT" Value=""/><br/>
<Item Name="DISCOUNT" Value=""/><br/>
<Item Name="PENALTY" Value=""/><br/>
<Item Name="INTEREST" Value=""/><br/>
<Item Name="COURT_FEE" Value=""/><br/>
<Item Name="SHERRIF_FEE" Value=""/><br/>
<Item Name="COMM_FEE" Value=""/><br/>
</Payment><br/>
</PaymentList><br/>
</TAXPYMT>
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none The XSD:
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none <?xml version="1.0" encoding="utf-8"?><br/>
<xs:schema id="TAXPYMT" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata <br/>
<xs:element name="Item <br/>
<xs:complexType><br/>
<xs:attribute name="Name" type="xs:string" /><br/>
<xs:attribute name="Value" type="xs:string" /><br/>
</xs:complexType><br/>
</xs:element><br/>
<xs:element name="TAXPYMT" msdata:IsDataSet="true" msdata:UseCurrentLocale="true <br/>
<xs:complexType><br/>
<xs:choice minOccurs="0" maxOccurs="unbounded <br/>
<xs:element ref="Item" /><br/>
<xs:element name="Header <br/>
<xs:complexType><br/>
<xs:sequence><br/>
<xs:element ref="Item" minOccurs="0" maxOccurs="unbounded" /><br/>
<xs:element name="Date" minOccurs="0" maxOccurs="unbounded <br/>
<xs:complexType><br/>
<xs:attribute name="Name" type="xs:string" /><br/>
<xs:attribute name="Year" type="xs:string" /><br/>
<xs:attribute name="Month" type="xs:string" /><br/>
<xs:attribute name="Day" type="xs:string" /><br/>
<xs:attribute name="Hour" type="xs:string" /><br/>
<xs:attribute name="Minute" type="xs:string" /><br/>
<xs:attribute name="Second" type="xs:string" /><br/>
<xs:attribute name="Millisecond" type="xs:string" /><br/>
</xs:complexType><br/>
</xs:element><br/>
</xs:sequence><br/>
</xs:complexType><br/>
</xs:element><br/>
<xs:element name="PaymentList <br/>
<xs:complexType><br/>
<xs:sequence><br/>
<xs:element name="Payment" minOccurs="0" maxOccurs="unbounded <br/>
<xs:complexType><br/>
<xs:sequence><br/>
<xs:element ref="Item" minOccurs="0" maxOccurs="unbounded" /><br/>
</xs:sequence><br/>
</xs:complexType><br/>
</xs:element><br/>
</xs:sequence><br/>
</xs:complexType><br/>
</xs:element><br/>
</xs:choice><br/>
</xs:complexType><br/>
</xs:element><br/>
</xs:schema>
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none The Generated class:
<p style="vertical-align:baseline; list-style-type:none


<p style="vertical-align:baseline; list-style-type:none //------------------------------------------------------------------------------<br/>
// <auto-generated><br/>
// This code was generated by a tool.<br/>
// Runtime Version:4.0.30319.296<br/>
//<br/>
// Changes to this file may cause incorrect behavior and will be lost if<br/>
// the code is regenerated.<br/>
// </auto-generated><br/>
//------------------------------------------------------------------------------<br/>
<br/>
using System.Xml.Serialization;<br/>
<br/>
// <br/>
// This source code was auto-generated by xsd, Version=4.0.30319.1.<br/>
// <br/>
<br/>
<br/>
/// <remarks/><br/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]<br/>
[System.SerializableAttribute()]<br/>
[System.Diagnostics.DebuggerStepThroughAttribute()]<br/>
[System.ComponentModel.DesignerCategoryAttribute("code")]<br/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]<br/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]<br/>
public partial class Item {<br/>
<br/>
private string nameField;<br/>
<br/>
private string valueField;<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Name {<br/>
get {<br/>
return this.nameField;<br/>
}<br/>
set {<br/>
this.nameField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Value {<br/>
get {<br/>
return this.valueField;<br/>
}<br/>
set {<br/>
this.valueField = value;<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]<br/>
[System.SerializableAttribute()]<br/>
[System.Diagnostics.DebuggerStepThroughAttribute()]<br/>
[System.ComponentModel.DesignerCategoryAttribute("code")]<br/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]<br/>
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]<br/>
public partial class TAXPYMT {<br/>
<br/>
private object[] itemsField;<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlElementAttribute("Header", typeof(TAXPYMTHeader), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]<br/>
[System.Xml.Serialization.XmlElementAttribute("Item", typeof(Item))]<br/>
[System.Xml.Serialization.XmlElementAttribute("PaymentList", typeof(TAXPYMTPaymentList), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]<br/>
public object[] Items {<br/>
get {<br/>
return this.itemsField;<br/>
}<br/>
set {<br/>
this.itemsField = value;<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]<br/>
[System.SerializableAttribute()]<br/>
[System.Diagnostics.DebuggerStepThroughAttribute()]<br/>
[System.ComponentModel.DesignerCategoryAttribute("code")]<br/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]<br/>
public partial class TAXPYMTHeader {<br/>
<br/>
private Item[] itemField;<br/>
<br/>
private TAXPYMTHeaderDate[] dateField;<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlElementAttribute("Item")]<br/>
public Item[] Item {<br/>
get {<br/>
return this.itemField;<br/>
}<br/>
set {<br/>
this.itemField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlElementAttribute("Date", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]<br/>
public TAXPYMTHeaderDate[] Date {<br/>
get {<br/>
return this.dateField;<br/>
}<br/>
set {<br/>
this.dateField = value;<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]<br/>
[System.SerializableAttribute()]<br/>
[System.Diagnostics.DebuggerStepThroughAttribute()]<br/>
[System.ComponentModel.DesignerCategoryAttribute("code")]<br/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]<br/>
public partial class TAXPYMTHeaderDate {<br/>
<br/>
private string nameField;<br/>
<br/>
private string yearField;<br/>
<br/>
private string monthField;<br/>
<br/>
private string dayField;<br/>
<br/>
private string hourField;<br/>
<br/>
private string minuteField;<br/>
<br/>
private string secondField;<br/>
<br/>
private string millisecondField;<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Name {<br/>
get {<br/>
return this.nameField;<br/>
}<br/>
set {<br/>
this.nameField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Year {<br/>
get {<br/>
return this.yearField;<br/>
}<br/>
set {<br/>
this.yearField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Month {<br/>
get {<br/>
return this.monthField;<br/>
}<br/>
set {<br/>
this.monthField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Day {<br/>
get {<br/>
return this.dayField;<br/>
}<br/>
set {<br/>
this.dayField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Hour {<br/>
get {<br/>
return this.hourField;<br/>
}<br/>
set {<br/>
this.hourField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Minute {<br/>
get {<br/>
return this.minuteField;<br/>
}<br/>
set {<br/>
this.minuteField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Second {<br/>
get {<br/>
return this.secondField;<br/>
}<br/>
set {<br/>
this.secondField = value;<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlAttributeAttribute()]<br/>
public string Millisecond {<br/>
get {<br/>
return this.millisecondField;<br/>
}<br/>
set {<br/>
this.millisecondField = value;<br/>
}<br/>
}<br/>
}<br/>
<br/>
/// <remarks/><br/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]<br/>
[System.SerializableAttribute()]<br/>
[System.Diagnostics.DebuggerStepThroughAttribute()]<br/>
[System.ComponentModel.DesignerCategoryAttribute("code")]<br/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]<br/>
public partial class TAXPYMTPaymentList {<br/>
// TAH - need to change this from [][] to []<br/>
private Item[][] paymentField;<br/>
<br/>
/// <remarks/><br/>
[System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]<br/>
[System.Xml.Serialization.XmlArrayItemAttribute("Item", typeof(Item), IsNullable=false)]
<p style="vertical-align:baseline; list-style-type:none public Item[][] Payment {<br/>
get {<br/>
return this.paymentField;<br/>
}<br/>
set {<br/>
this.paymentField = value;<br/>
}<br/>
}<br/>
}

<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none
<p style="vertical-align:baseline; list-style-type:none


<div style="margin:0px 0.25em 0px 0px; padding:0px; border:0px; font-size:14px; vertical-align:baseline; color:#666666; font-family:Segoe UI,Helvetica,Garuda,Arial,sans-serif; line-height:21px; float:right!important


View the full article
 
Back
Top