コード例 #1
0
ファイル: CarService.cs プロジェクト: Cornevur/WCFTutorial
        public Car GetCar(int Id)
        {
            var file = ConfigurationManager.AppSettings["fileCar"];
            var doc = XDocument.Load(file);

            var result = new Car();

            var element = doc.Descendants("Car").FirstOrDefault(x => x.Attribute("Id").Value == Id.ToString());

            var type = element.Attribute("Type").Value;
            switch (type)
            {
                case "Truck":
                    result = new TruckCar();
                    ((TruckCar)result).Capacity = double.Parse(element.Element("Capacity").Value, CultureInfo.GetCultureInfo("en-US"));
                    break;

                case "Passenger":
                    result = new PassengerCar();
                    ((PassengerCar)result).Passengers = int.Parse(element.Element("Passengers").Value, CultureInfo.GetCultureInfo("en-US"));
                    break;
                default:
                    break;
            }

            result.Id = int.Parse(element.Attribute("Id").Value);
            result.Vendor = element.Element("Vendor").Value;
            result.Model = element.Element("Model").Value;
            result.Year = int.Parse(element.Element("Year").Value);

            return result;
        }
コード例 #2
0
        public Car GetCar(int parId)
        {
            var id = parId;

            var file = ConfigurationManager.AppSettings["fileCar"];

            Car result;

            var doc = XDocument.Load(file);

            var element = doc.Descendants("Car").FirstOrDefault(x => x.Attribute("Id").Value == id.ToString());

            switch (element.Attribute("Type").Value)
            {
            case "Passenger":
                result = new PassengerCar {
                    Passengers = int.Parse(element.Element("Passengers").Value, CultureInfo.GetCultureInfo("en-US"))
                };
                break;

            case "Truck":
                result = new TruckCar {
                    Capacity = double.Parse(element.Element("Capacity").Value, CultureInfo.GetCultureInfo("en-US"))
                };
                break;

            default:
                result = new Car();
                break;
            }

            result.Id     = int.Parse(element.Attribute("Id").Value);
            result.Vendor = element.Element("Vendor").Value;
            result.Model  = element.Element("Model").Value;
            //result.Year = int.Parse(element.Element("Year").Value);
            result.MileAge = element.Element("MileAge") != null?int.Parse(element.Element("MileAge").Value) : 0;

            return(result);
        }