How to convert XML/JSON file to C# class?

前端 未结 4 1033
不知归路
不知归路 2020-12-02 23:59

I have XML file like this:



    

        
4条回答
  •  醉话见心
    2020-12-03 00:29

    You can follow this simple step

    1.Please Add using System.Xml as a reference;
    2.Make a class named book in this way
    
    
    
         public class book
                {
                    public Nullable date{ get; set; }
                    public decimal price { get; set; }
                    public string title { get; set; }
                    public string description { get; set; }
            }
    
        try
                    {
                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.Load("Write down full path");
                        XmlNodeList dataNodes = xmlDoc.SelectNodes("/catalog");
    
                        foreach (XmlNode node in dataNodes)
                        {
                            book objbook = new book();
                         objbook.date=Convert.ToDateTime(node.Attributes["date"].Value);
                           objbook.title=node.SelectSingleNode("title").InnerText;
                       objbook.description=node.SelectSingleNode("description").InnerText;
    objbook.price=Convert.ToDecimal(node.SelectSingleNode("price").InnerText);
    
                        }
    
                    }
    catch(Exception ex)
    {
    throw ex;
    }
    

提交回复
热议问题