Reputation: 5745
I might going wrong direction, but I want to create generic classes for following XML structure.
<?xml version="1.0" encoding="UTF-8"?>
<root>
<success>true</success>
<data>
<item>
<Barcode>20450004941980</Barcode>
<ChildDocuments>
<success>true</success>
<data>
<item>
<StateId>10</StateId>
</item>
</data>
<errors />
<warnings />
<info />
</ChildDocuments>
</item>
</data>
<errors />
<warnings />
<info />
</root>
There could be more elements in as well as in ChildDocuments. I've created following structure:
[Serializable()]
[System.Xml.Serialization.XmlRoot("root")]
public class XmlRoot<T>
{
public XmlRoot()
{
DataArray = new List<T>();
}
[XmlElement("data")]
public List<T> DataArray { get; set; }
}
[Serializable()]
public class XmlRootData<T>
{
public XmlRootData()
{
ItemArray = new List<T>();
}
[XmlElement("item")]
public List<T> ItemArray { get; set; }
}
and now I am thinking how to create something generic for ChildDocument. Basically it has the same generic structure as the root document.So far I know that there could be only 1 level for ChildDocument, so I can create 2 more generic classes, something like:
[Serializable()]
public class XmlRootData<T,U>
{
public XmlRootData()
{
ItemArray = new List<T>();
}
[XmlElement("item")]
public List<T> ItemArray { get; set; }
public List<XmlRoot<U>> ChildDataRoot { get; set; }
}
I can create 2 more extra classes for Child inner xml, however I am not 100% sure that there could be max 2 levels...
Upvotes: 0
Views: 503
Reputation: 3071
If generating class thru tools for your xml is an option then you can follow below steps. It will help if you have real xml file with complete data. I have given a shot based on the xml file passed in the question.
And below is the generated Class, now based on this class you can adjust your class or use the below class as is. You can also rename the properties and customize all the names by decorating them with appropriate attributes.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.42000
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.81.0.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[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 root {
private bool successField;
private rootData dataField;
private object errorsField;
private object warningsField;
private object infoField;
/// <remarks/>
public bool success {
get {
return this.successField;
}
set {
this.successField = value;
}
}
/// <remarks/>
public rootData data {
get {
return this.dataField;
}
set {
this.dataField = value;
}
}
/// <remarks/>
public object errors {
get {
return this.errorsField;
}
set {
this.errorsField = value;
}
}
/// <remarks/>
public object warnings {
get {
return this.warningsField;
}
set {
this.warningsField = value;
}
}
/// <remarks/>
public object info {
get {
return this.infoField;
}
set {
this.infoField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootData {
private rootDataItem itemField;
/// <remarks/>
public rootDataItem item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItem {
private ulong barcodeField;
private rootDataItemChildDocuments childDocumentsField;
/// <remarks/>
public ulong Barcode {
get {
return this.barcodeField;
}
set {
this.barcodeField = value;
}
}
/// <remarks/>
public rootDataItemChildDocuments ChildDocuments {
get {
return this.childDocumentsField;
}
set {
this.childDocumentsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocuments {
private bool successField;
private rootDataItemChildDocumentsData dataField;
private object errorsField;
private object warningsField;
private object infoField;
/// <remarks/>
public bool success {
get {
return this.successField;
}
set {
this.successField = value;
}
}
/// <remarks/>
public rootDataItemChildDocumentsData data {
get {
return this.dataField;
}
set {
this.dataField = value;
}
}
/// <remarks/>
public object errors {
get {
return this.errorsField;
}
set {
this.errorsField = value;
}
}
/// <remarks/>
public object warnings {
get {
return this.warningsField;
}
set {
this.warningsField = value;
}
}
/// <remarks/>
public object info {
get {
return this.infoField;
}
set {
this.infoField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocumentsData {
private rootDataItemChildDocumentsDataItem itemField;
/// <remarks/>
public rootDataItemChildDocumentsDataItem item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class rootDataItemChildDocumentsDataItem {
private byte stateIdField;
/// <remarks/>
public byte StateId {
get {
return this.stateIdField;
}
set {
this.stateIdField = value;
}
}
}
Upvotes: 1