locked
Convert xml to class object RRS feed

  • Question

  • User1230414635 posted

    <div class="container"> <div class="body">

    Hi,

    I need to convert xml to class oject. Attached my xml format. This xml I need to convert as List<Class>.

    Classname test = new Classname();

    List<ClsData> data= test.ClsDataList;

    Thanks...

    My xml format receiving from service:

    xml

    Saturday, October 10, 2015 5:31 AM

Answers

  • User-986267747 posted

    Hi santoshhegde,

    I need to convert xml to class oject. Attached my xml format. This xml I need to convert as List<Class>.

    According to your description, I create a sample, it works well. You could refer to the following code.

    XML

    <?xml version="1.0" encoding="utf-8" ?>
    <ClsFrontEndFormData>
      <GetBndlChildList>
        <ClsGetBndChild>
          <PicsBndlFormChildFormId>1234</PicsBndlFormChildFormId>
          <PicsBndlFormChildAss>0</PicsBndlFormChildAss>
          <PicsBndlFormChildConf>1</PicsBndlFormChildConf>
        </ClsGetBndChild>
        <ClsGetBndChild>
          <PicsBndlFormChildFormId>5678</PicsBndlFormChildFormId>
          <PicsBndlFormChildAss>1</PicsBndlFormChildAss>
          <PicsBndlFormChildConf>2</PicsBndlFormChildConf>
        </ClsGetBndChild>
      </GetBndlChildList>
    </ClsFrontEndFormData>
    
    

    ClsData

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    
    namespace XmltoClassList
    {
        [XmlRoot("ClsFrontEndFormData")]
        public class ClsData
        {
            [XmlArray("GetBndlChildList"), XmlArrayItem("ClsGetBndChild")]
            public ClsGetBndChild[] ClsGetBndChilds { get; set; }
        }
    }
    

    ClsGetBndChild

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    
    namespace XmltoClassList
    {
       [XmlRoot("ClsGetBndChild")]
       public class ClsGetBndChild
        {
           [XmlElement("PicsBndlFormChildFormId")]
           public string PicsBndlFormChildFormId { get; set; }
    
           [XmlElement("PicsBndlFormChildAss")]
           public string PicsBndlFormChildAss { get; set; }
    
           [XmlElement("PicsBndlFormChildConf")]
           public string PicsBndlFormChildConf { get; set; }
        }
    }
    

    This is the console application to test my code.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Xml.Serialization;
    
    namespace XmltoClassList
    {
        class Program
        {
            static void Main(string[] args)
            {
                try
                {
                    using (StreamReader sr = new StreamReader(@"../../XMLFile1.xml"))
                    {
                        
                        XmlSerializer xmldes = new XmlSerializer(typeof(ClsData));
                        ClsData cls=xmldes.Deserialize(sr) as ClsData;
                        List<ClsGetBndChild> data = cls.ClsGetBndChilds.ToList<ClsGetBndChild>();
                    }
                }
                catch (Exception e)
                {
    
                   
                }
    
            }
        }
    }
    

    I hope it's helpful to you.

    Best Regards,

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 12, 2015 12:22 AM