1

I created a XML model for the purpose of converting SQL resultset to XML file. My model class includes inner classes.

Here is the sample model class for xml.

public  class mymodel
{
    private const string dec= "declaration",
        abc="declaration";
    [XmlElement("Item1", Namespace = dec)]
    public string Item1{ get; set; }

    [XmlRoot("InnerClass", Namespace = dec)]
    public class InnerClass
    {
        [XmlElement("InnerItem", Namespace = abc)]
        public string InnerItem{ get; set; }
    }
}

Here is my prepared code to convert

DataSet Ds =Get_DataSet_from_Query("Select * from something");
mymodel DpAdv = null;
mymodel InnerClass=null;
for (int i = 0; i < Ds.Tables[0].Rows.Count; i++)
{
    DpAdv = new mymodel
    {
        Item1= Ds.Tables[0].Rows[i]["ID"].ToString(),
    };
    InnerClass= new mymodel.InnerClass
    {
        InnerItem= Ds.Tables[0].Rows[i]["ID"].ToString(),
    };
}

And I Called ConvertXML function and passed parameter 'DpAdv' Object

ConvertXML(DpAdv);

Inner Class item Data are not included and missing.

Here is my xml output file . There is missing inner class data.

<?xml version="1.0" encoding="utf-8"?>
<DespatchAdvice xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:dec="declaration" >
  <dec:Item1>14126</cbc:ID>
</DespatchAdvice>

How can I add all data including inner class data to XML file.

Niranjan Singh
  • 18,017
  • 2
  • 42
  • 75
Zuly
  • 37
  • 7
  • See here for some ideas on classes https://stackoverflow.com/q/1083032/125981 Use of classes appears to be your issue here. Please use Pascal case for classes: `public class Mymodel` not `public class mymodel` Use UPPER CASE for constants `DEC` and `ABC` not `dec` and `abc`. Note here how you assign an upper case variable `mymodel InnerClass = null;` with the `InnerClass` as an `mymodel` class - with the case issue you can see how you have even confused yourself here. – Mark Schultheiss Jun 19 '18 at 11:52

2 Answers2

0

For now I 've got it. I should declare variable for inner class

 [XmlElement( ElementName = "Item1", DataType = "string", Namespace = DEC)]
    public InnerClass Item1{ get; set; }
Zuly
  • 37
  • 7
-1

You need to use the classes properly perhaps? Classes normally have Pascal case. FWIW, I did this really fast, did not check for minor issues here...or even compile this.

public class Mymodel
{
    // const normally are UPPERCASE
    private const string DEC = "declaration";
    private const string ABC= "declaration";

    public class InnerClass
    {
        [XmlElement( ElementName = "InnerItem", typeof(string), Namespace = ABC)]
        public string InnerItem { get; set; }
    }

    [XmlElement( ElementName = "Item1", DataType = "string", Namespace = DEC)]
    public string Item1{ get; set; }


    public Mymodel
    {
    }
    public Mymodel(string item, InnerClass inner)
    {
        this.Item1 = item;
        this.InnerClass = inner;
    }
}

Class use:

DataSet ds = Get_DataSet_from_Query("Select * from something");
List<Mymodel> dpAdvs = new List<Mymodel>();
Mymodel dpAdv = null;
var table = ds.Tables[0];

foreach (DataRow dr in table.Rows)
{
    Mymodel dpAdv = new Mymodel();
    dpAdv.Item1 = dr["ID"].ToString();

    dpAdv.InnerClass = new Mymodel.InnerClass();
    dpAdv.InnerClass.InnerItem = dr["ID"].ToString(),

    dpAdvs.Add(dpAdv);
}


// Define the root element
var serializer = new XmlSerializer(typeof(List<Mymodel>),
                                   new XmlRootAttribute("Mymodels"));

// or use it like this
XElement xmlElements = new XElement("Mymodels", dpAdvs.Select(i => new XElement("dpAdv", i)));
Mark Schultheiss
  • 32,614
  • 12
  • 69
  • 100