Friday, 8 May 2015

Check or Create Xml file

 if (!File.Exists(Server.MapPath("first.xml")))
        {
            XmlTextWriter wrt = new XmlTextWriter(Server.MapPath("first.xml"), System.Text.Encoding.UTF8);
            wrt.WriteStartDocument(true);
            wrt.Formatting = Formatting.Indented;
            wrt.WriteStartElement("root");
            wrt.WriteEndElement();
            wrt.Close();
        }

XML search node or update node

  protected void btnnode_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        string path = Server.MapPath("first.xml");
        xdoc.Load(path);
        XmlNodeList xnlist = xdoc.SelectNodes("root/details");
        foreach (XmlNode xn in xnlist)
        {
            string s = xn["name"].InnerText;
            if (xn["name"].InnerText == "hello")
            {
               // xn["name"].Attributes.Append(xdoc.CreateAttribute("id", "1"));
                xn["name"].InnerText = "hiii2";
            }
        }
        xdoc.Save(path);

    }