locked
Correct XML class? RRS feed

  • Question

  • User330911564 posted

    I just wanted to know if this class is created well. if not, i'm open for correction and ideas. thanks in advance

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Xml;
    
    
    namespace XmlClassie.Class
    {
        public class CarClass
        {
            public int unqNr { get; set; }
            public DateTime lastM { get; set; }
            public string name { get; set; }
            public string email { get; set; }
            public string CarNr { get; set; }
            public string CarCompany { get; set; }
            public string CarModel { get; set; }
    
            public string BigImg { get; set; }
            public string SmallImg { get; set; }
        }
    
        public class XMLHelper
        {
            public List<CarClass> GetList()
            {
                XmlDocument settingDoc = new XmlDocument();
                settingDoc.Load(HttpContext.Current.Server.MapPath("StarCar.xml"));
                XmlNodeList adstarcars = settingDoc.SelectNodes("/Cars/adStarCar");
                if (adstarcars != null)
                {
                    var listOfAdStarCar = new List<CarClass>();
                    var newAdStarCar = new CarClass();
                    foreach (XmlNode adstarcar in adstarcars)
                    {
                        //UnqNr
                        var unqNum = adstarcar.SelectSingleNode("unqNumber");
                        newAdStarCar.unqNr = Convert.ToInt32(adstarcar.SelectSingleNode("unqNumber").InnerText);
    
                        //lastMute
                        var lastMute = adstarcar.SelectSingleNode("lastMute");
                        newAdStarCar.lastM = Convert.ToDateTime(adstarcar.SelectSingleNode("lastMute").InnerText);
                        //
                    }
                }
                return GetList();
            }
        }
    }
    
    
    

    Wednesday, March 8, 2017 8:01 AM

Answers

  • User527778624 posted

    Hi,

    CarClass seems fine, make sure it matches StarCar.xml schema.

    GetList() method seems it need correction.

    public List<CarClass> GetList()
    {
    	XmlDocument settingDoc = new XmlDocument();
    	settingDoc.Load(HttpContext.Current.Server.MapPath("StarCar.xml"));
    	XmlNodeList adstarcars = settingDoc.SelectNodes("/Cars/adStarCar");
    	var listOfAdStarCar = new List<CarClass>();
    	if (adstarcars != null)
    	{		
    		foreach (XmlNode adstarcar in adstarcars)
    		{		
    			var newAdStarCar = new CarClass();
    			//UnqNr
    			var unqNum = adstarcar.SelectSingleNode("unqNumber");
    			if(unqNum != null) 
    			{
    				newAdStarCar.unqNr = Convert.ToInt32(unqNum.InnerText);
    			}
    			//lastMute
    			var lastMute = adstarcar.SelectSingleNode("lastMute");
    			if(lastMute != null) 
    			{
    				newAdStarCar.lastM = Convert.ToDateTime(lastMute.InnerText);
    			}
    			
    			// ....
    			
    			listOfAdStarCar.Add(newAdStarCar);
    		}
    	}
    	return listOfAdStarCar;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 8, 2017 8:22 AM
  • User527778624 posted

    Hi,

    and i want to call this in my page load, how do i do that?

    something like this:

    using XmlClassie.Class;
    
    public void Page_Load(object sender, EventArgs e)
    {
    	if(!IsPostBack)
    	{
    		var obj = new XMLHelper();
    		var carList = obj.GetList();
    		//now use carList
    	}
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 8, 2017 12:46 PM

All replies

  • User527778624 posted

    Hi,

    CarClass seems fine, make sure it matches StarCar.xml schema.

    GetList() method seems it need correction.

    public List<CarClass> GetList()
    {
    	XmlDocument settingDoc = new XmlDocument();
    	settingDoc.Load(HttpContext.Current.Server.MapPath("StarCar.xml"));
    	XmlNodeList adstarcars = settingDoc.SelectNodes("/Cars/adStarCar");
    	var listOfAdStarCar = new List<CarClass>();
    	if (adstarcars != null)
    	{		
    		foreach (XmlNode adstarcar in adstarcars)
    		{		
    			var newAdStarCar = new CarClass();
    			//UnqNr
    			var unqNum = adstarcar.SelectSingleNode("unqNumber");
    			if(unqNum != null) 
    			{
    				newAdStarCar.unqNr = Convert.ToInt32(unqNum.InnerText);
    			}
    			//lastMute
    			var lastMute = adstarcar.SelectSingleNode("lastMute");
    			if(lastMute != null) 
    			{
    				newAdStarCar.lastM = Convert.ToDateTime(lastMute.InnerText);
    			}
    			
    			// ....
    			
    			listOfAdStarCar.Add(newAdStarCar);
    		}
    	}
    	return listOfAdStarCar;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 8, 2017 8:22 AM
  • User330911564 posted

    thank you very much, 

    and i want to call this in my page load, how do i do that?

    Wednesday, March 8, 2017 9:40 AM
  • User527778624 posted

    Hi,

    and i want to call this in my page load, how do i do that?

    something like this:

    using XmlClassie.Class;
    
    public void Page_Load(object sender, EventArgs e)
    {
    	if(!IsPostBack)
    	{
    		var obj = new XMLHelper();
    		var carList = obj.GetList();
    		//now use carList
    	}
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 8, 2017 12:46 PM