BookMgnt.Models.ChangeInfoSite.SaveInfo C# (CSharp) Method

SaveInfo() public method

public SaveInfo ( string Address, string Email, string phone, string Footer, string imageName ) : void
Address string
Email string
phone string
Footer string
imageName string
return void
        public void SaveInfo(string Address, string Email, string phone, string Footer, string imageName)
        {
            XmlDocument newXmlDoc = new XmlDocument();
            XmlDeclaration declaration = newXmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);
            newXmlDoc.AppendChild(declaration);

            XmlElement itemsNode = newXmlDoc.CreateElement("Configuration");
            newXmlDoc.AppendChild(itemsNode);

            XmlElement item1 = newXmlDoc.CreateElement("address");
            item1.InnerText = Address;
            itemsNode.AppendChild(item1);

            XmlElement item2 = newXmlDoc.CreateElement("email");
            item2.InnerText = Email;
            itemsNode.AppendChild(item2);

            XmlElement item3 = newXmlDoc.CreateElement("phone");
            item3.InnerText = phone;
            itemsNode.AppendChild(item3);

            XmlElement item4 = newXmlDoc.CreateElement("footer");
            item4.InnerText = Footer;
            itemsNode.AppendChild(item4);

            XmlElement item5 = newXmlDoc.CreateElement("banner");
            item5.InnerText = imageName;
            itemsNode.AppendChild(item5);

            //if (File.Exists(filePath))
            //{
            //    File.SetAttributes(filePath, FileAttributes.Normal);
            //    FileIOPermission filePermission =
            //             new FileIOPermission(FileIOPermissionAccess.AllAccess, filePath);

            //    FileStream ContentSite = new FileStream(filePath, FileMode.Create);

            //    XmlWriter w = XmlWriter.Create(ContentSite);
            //}

            newXmlDoc.Save(filePath); // name of the new .xml file
        }

Usage Example

Example #1
0
        public ActionResult SiteInfo(InfoSite info, HttpPostedFileBase image_banner)
        {
            if (ModelState.IsValid)
            {
                ChangeInfoSite ch = new ChangeInfoSite();
                if (image_banner != null && image_banner.ContentLength > 0)
                {
                    string pic = System.IO.Path.GetFileName(image_banner.FileName);
                    string path = System.IO.Path.Combine(Server.MapPath("~/Content/image"), pic);
                    if (System.IO.File.Exists(path))
                    {
                        int count = 2;
                        while (System.IO.File.Exists(path))
                        {
                            pic = count.ToString() + pic;
                            path = System.IO.Path.Combine(Server.MapPath("~/Content/image"), pic);
                        }
                    }
                    image_banner.SaveAs(path);
                    info.imageName = pic;
                    //ch.SaveInfo(info.Address, info.Email, info.Phone, info.Footer, info.imageName);
                }
                ch.SaveInfo(info.Address, info.Email, info.Phone, info.Footer, info.imageName);

                return RedirectToAction("Contact", "Home");
            }
            return View(info);
        }