Friday, January 15, 2010

Writing on an existing XML file

below your XML files named as someting.xml

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfSlide xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://MYDOMAINHERE.com/">
  <Slide>
    <ImagePath>/images/polperro/generic_1024.jpg</ImagePath>
    <Name> images_15seconds</Name>
    <Description />
  </Slide>
  <Slide>
    <ImagePath>/Images/HeaderImages/header.jpg</ImagePath>
    <Name>header.jpg</Name>
    <Description />
  </Slide>
</ArrayOfSlide>

Now this function will add contents in this XML file.

private void xmlUpdate(string filename)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Server.MapPath(@"~\XML\XMLImagePath.xml"));
            string ns = xDoc.DocumentElement.NamespaceURI;

            XmlNode root = xDoc.DocumentElement;

            XmlElement newElement = xDoc.CreateElement("Slide", ns);

            XmlElement childElement1 = xDoc.CreateElement("ImagePath", ns);

            childElement1.InnerText = @"/Images/HeaderImages/" + filename;

            XmlElement childElement2 = xDoc.CreateElement("Name", ns);
            childElement2.InnerText = filename;

            XmlElement childElement3 = xDoc.CreateElement("Description", ns);

            newElement.AppendChild(childElement1);
            newElement.AppendChild(childElement2);
            newElement.AppendChild(childElement3);

            root.AppendChild(newElement);

            xDoc.Save(Server.MapPath(@"~\XML\XMLImagePath.xml"));
            lblSuccMsg.Text = "Successfully appended to the Xml file.";
        }

Similarly deleting a Node from the XML file....

private void xmlDelete(string ImgPath)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.Load(Server.MapPath(@"~\XML\XMLImagePath.xml"));
            XmlNode root = xDoc.DocumentElement;

            XmlNodeList xmlNdList = xDoc.GetElementsByTagName("Slide");

            for (int i = 0; i < xmlNdList.Count; i++)
            {

                if (xmlNdList.Item(i).Name == "Slide")
                {
                    if (xmlNdList.Item(i).ChildNodes.Item(0).InnerText.Trim() == ImgPath)
                    {
                        root.RemoveChild(xmlNdList.Item(i));
                    }
                }
            }

            xDoc.Save(Server.MapPath(@"~\XML\XMLImagePath.xml"));
          
        }


Similarly reading from that XML file......





private string[,] xmlData()
        {
            string[,] ImagePaths;

            XmlDocument xDocm = new XmlDocument();
            xDocm.Load(Server.MapPath(@"~\XML\XMLImagePath.xml"));
            XmlNodeList xmlNdList = xDocm.GetElementsByTagName("Slide");
            ImagePaths = new string[xmlNdList.Count, 2];

            for (int i = 0; i < xmlNdList.Count; i++)
            {

                if (xmlNdList.Item(i).Name == "Slide")
                {
                    ImagePaths[i, 0] = xmlNdList.Item(i).ChildNodes.Item(0).InnerText;
                    ImagePaths[i, 1] = xmlNdList.Item(i).ChildNodes.Item(1).InnerText;
                }
            }

            return ImagePaths;

        }

No comments:

Post a Comment