Sunday, August 11, 2013

How to write Data In XML with some attributes

                string dir = HttpContext.Current.Server.MapPath("~/Errors/");
                string FileName = HttpContext.Current.Server.MapPath("~/Errors/AKAARERRORFILE.xml");

                string date = DateTime.Now.Day.ToString() + "-" + DateTime.Now.Month.ToString() + "-" +                     DateTime.Now.Year.ToString();
                XmlTextReader reader = new XmlTextReader(FileName);
                XmlDocument doc = new XmlDocument();
                doc.Load(reader);
                reader.Close();

                XmlElement parentelement = doc.CreateElement("Date");
                XmlAttribute dateatt=doc.CreateAttribute("date");
                dateatt.Value = date;
                parentelement.Attributes.Append(dateatt);
                XmlElement thepage = doc.CreateElement("page");
                thepage.InnerText = page;
                XmlElement thefunction = doc.CreateElement("fun");
                thefunction.InnerText = function;
                XmlElement thedes = doc.CreateElement("des");
                thedes.InnerText = description;

                parentelement.AppendChild(thepage);
                parentelement.AppendChild(thefunction);
                parentelement.AppendChild(thedes);
                doc.DocumentElement.AppendChild(parentelement);

                doc.Save(FileName);

               Download Example

No comments:

Post a Comment