Quantcast
Channel: XML, System.Xml, MSXML and XmlLite forum
Viewing all articles
Browse latest Browse all 935

XML serialize

$
0
0

I am trying to generate an xml by serializing and facing some issues. There is a part of xml which is dynamic. the section "person" is dynamic i.e the sub sections
 person can increase or decrease. I am able to serialie the class with static value( with single person name and age) but when getting multiple 
values then I am not able to handle that.All the values are fetched from database and every name and age is store as a distinct row in database.So if I have 2 names and age then it will be stored
as 2 distinct rows in database. I tried to create a loop(on basis of number of rows) but not able understand how to incorporate the dynamic section to rest of xml
part.

Can someone help on this?

--------------- desired xml output--------------------------------------

   

<details>
 <description>Some description</description>
 <hobby> Anything</hobby>

<person>
   <name>Dann</name>
   <age>21</age>
</person>
<person>
   <name>Scott</name>
   <age>23</name>
</person>


</details>

--------------------------------------------------CODE -----------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Xml.Serialization;

namespace xmltest
{
    public class Program
    {
        public class xmlserial
        {

            [XmlElement("details")]
            public string details { get; set; }
            [XmlElement("description")]
            public string description { get; set; }
            [XmlElement("hobby")]
            public string hobby { get; set; }

            [XmlElement("person")]
            public string person { get; set; }
            [XmlElement("name")]
            public string name { get; set; }
            [XmlElement("age")]
            public int age { get; set; }

            public xmlserial()
            {
            }

        }
        static void Main(string[] args)
        {

            string conStr = ConfigurationManager.ConnectionStrings["Test"].ToString();
            DataSet abc = new DataSet();
            abc = test_DS(conStr);

            List<xmlserial> AddressList = new List<xmlserial>();
            xmlserial y = new xmlserial();
            foreach (DataTable dt in abc.Tables)
            {

                y.details = "hello";
                y.description = "description";
                y.hobby = "hobby";
                y.person = "person";
                foreach (DataRow dr in dt.Rows)
                {

                    y.name = dr["NAME"].ToString();
                    y.age =(int) dr["age"];

                }
                AddressList.Add(y);
            }
            serialize(AddressList);

        }
        public static void serialize(List<xmlserial> list)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(List<xmlserial>));
            serializer.Serialize(Console.Out, list);
            Console.WriteLine();
            Console.ReadLine();

        }

        public static DataSet test_DS(string conStr)
        {
            try
            {
                using (SqlConnection SqlConn = new SqlConnection(conStr))
                {

                    SqlConn.Open();

                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.Connection = SqlConn;

                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.CommandText = "dbo.test_sp";                      

                        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                        {                      
                            DataSet dataset = new DataSet();
                            da.Fill(dataset);

                            return dataset;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //Catch the exception

            }
            return null;
        }

    }
}

--------------------------------------------------------------------------------SQL  table and Procedure ---------------------------------

create table persondetails(name varchar(20),age int)
insert into persondetails(name,age)
select 'Dann',21
union all
select 'Scott',23
union all
Select 'Allen',24



create proc test_sp
as
 begin
   select distinct * from persondetails
 end

                                                                                       

Viewing all articles
Browse latest Browse all 935

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>