public String getSpots() { List <SpotModel> list = new List <SpotModel>(); XmlDocument xmlDoc = new XmlDocument(); String spotXml = HttpContext.Current.Server.MapPath("~/App_Data/Spot.xml"); xmlDoc.Load(spotXml); XmlNodeList spotList = xmlDoc.SelectNodes("/Spots[@*]/Spot"); foreach (XmlNode spotNode in spotList) { SpotModel spot = new SpotModel(); // get name spot.name = Security.Decryption(spotNode.Attributes["Name"].Value); // get image String path = HttpContext.Current.Server.MapPath("~/Thumbnail/") + spot.name + ".jpg"; Image image = Image.FromFile(path); MemoryStream stream = new MemoryStream(); image.Save(stream, ImageFormat.Jpeg); stream.Position = 0; byte[] array = stream.ToArray(); spot.imgBytes = array; //spot.imgStr = Encoding.UTF8.GetString(array,0,array.Length); // get location SpotLocation location = new SpotLocation(); location.state = Security.Decryption(spotNode.SelectSingleNode("Location").SelectSingleNode("State").InnerText); location.city = Security.Decryption(spotNode.SelectSingleNode("Location").SelectSingleNode("City").InnerText); location.zipcode = Security.Decryption(spotNode.SelectSingleNode("Location").SelectSingleNode("ZipCode").InnerText); spot.location = location; //get introduction spot.introduction = Security.Decryption(spotNode.SelectSingleNode("Introduction").InnerText); //get price spot.price = Security.Decryption(spotNode.SelectSingleNode("Price").InnerText); list.Add(spot); } SpotRootObject rootObj = new SpotRootObject(); rootObj.list = list; String json = JsonConvert.SerializeObject(rootObj); return(json); }
public Boolean addSpot(String json) { try { SpotRootObject rootObj = JsonConvert.DeserializeObject <SpotRootObject>(json); SpotModel key = rootObj.key; //Add spot into spot.xml String spotXml = HttpContext.Current.Server.MapPath("~/App_Data/Spot.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(spotXml); //create a new spot element and set value or attribute XmlElement spot = xmlDoc.CreateElement("Spot"); spot.SetAttribute("Name", Security.Encryption(key.name)); xmlDoc.SelectSingleNode("/Spots").AppendChild(spot); // location XmlElement location = xmlDoc.CreateElement("Location"); spot.AppendChild(location); XmlElement state = xmlDoc.CreateElement("State"); state.InnerText = Security.Encryption(key.location.state); location.AppendChild(state); XmlElement city = xmlDoc.CreateElement("City"); city.InnerText = Security.Encryption(key.location.city); location.AppendChild(city); XmlElement zipcode = xmlDoc.CreateElement("ZipCode"); zipcode.InnerText = Security.Encryption(key.location.zipcode); location.AppendChild(zipcode); // image stream save as jpg file Image img = Image.FromStream(new MemoryStream(key.imgBytes)); var ratio = (double)225 / img.Height; int imgHeight = (int)(img.Height * ratio); int imgWidth = (int)(img.Width * ratio); Image.GetThumbnailImageAbort dCallBack = new Image.GetThumbnailImageAbort(ThumbnailCallback); Image thumbnailImg = img.GetThumbnailImage(imgWidth, imgHeight, dCallBack, IntPtr.Zero); String imgFile = key.name + ".jpg"; thumbnailImg.Save(Path.Combine(HttpContext.Current.Server.MapPath("~/Thumbnail"), imgFile), ImageFormat.Jpeg); thumbnailImg.Dispose(); // introduction XmlElement intro = xmlDoc.CreateElement("Introduction"); intro.InnerText = Security.Encryption(key.introduction); spot.AppendChild(intro); // price XmlElement price = xmlDoc.CreateElement("Price"); price.InnerText = Security.Encryption(key.price); spot.AppendChild(price); xmlDoc.Save(spotXml); return(true); } catch { return(false); } }