-1

I'm trying to figure out how I can go about updating my XML file. I know how to read and write, but no idea how to update an existing record.

My XML file looks like:

<?xml version="1.0" standalone="yes"?>
      <Categories>
       <Category>
        <CategoryId>1</CategoryId>
        <CategoryName>Ayourvedic</CategoryName>
       </Category>
      <Category>
      <CategoryId>2</CategoryId>
      <CategoryName>Daily Needs</CategoryName>
     </Category>
     <Category>
      <CategoryId>3</CategoryId>
      <CategoryName>Clothes</CategoryName>
     </Category>
     <Category>
      <CategoryId>4</CategoryId>
      <CategoryName>Shops</CategoryName>
     </Category>
     <Category>
     <CategoryId>5</CategoryId>
     <CategoryName>daily use product</CategoryName>
     </Category>
   </Categories>

and This is how I'm writing the file:

   private void btnUpdate_Click(object sender, EventArgs e)
    {
        XmlDocument xdoc = new XmlDocument();
        string PATH = "xmldata.xml";
        XElement xElement;
        xElement = new XElement("Category");

        XElement element = new XElement(
            "Category",
            new XAttribute("CategoryId", CategoryId),
            new XAttribute("CategoryName", CategoryName)

            );
        xElement.Add(element);
        xElement.Save("PATH");
    }

but my code is not working please any one can give some idea or solution.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2342574
  • 109
  • 2
  • 8
  • 17
  • I think you mean [4 *upvotes* 1 favorite](http://stackoverflow.com/questions/6046597/updating-xml-file-c-linq?rq=1). Anyway, the question is not clear at all. Which part you want to update for example? And before going to updating logic, have you been able to read/parse? if yes, then show that part, it would be more relevant – har07 Apr 26 '15 at 02:54
  • Might be duplicate. See: http://stackoverflow.com/questions/5975114/adding-new-node-to-existing-xmldocument-object ...same problem. Also this one for using System.Linq.Xml: http://stackoverflow.com/questions/17665753/add-new-xelement-to-xdocument – Jimmy Chandra Apr 26 '15 at 02:55
  • You are mismatching the usage of `System.Xml` (`XmlDocument`) and `System.Linq.Xml` (`XElement`). Please choose one or the other. – Jimmy Chandra Apr 26 '15 at 02:57
  • Please provide details on "my code is not working". – Enigmativity Apr 26 '15 at 03:00
  • You can't update an XML file. It's just a text file, so it's sequential. All you can do is read it into a document (XDocument, preferred) modify the document, then save the document. – John Saunders Apr 26 '15 at 04:11

1 Answers1

1

Using System.Xml The following code shall help:

    static void Main(string[] args)
    {
        String inputfile = @"D:\Temp\cat.xml";

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(inputfile);
        XmlNode root = xmldoc.DocumentElement;

        //Method 1
        XmlElement category = xmldoc.CreateElement("Category");
        XmlElement catid = xmldoc.CreateElement("CategoryId");
        XmlElement catname = xmldoc.CreateElement("CategoryName");

        catid.InnerText = "6";
        catname.InnerText = "The newly added category";

        category.AppendChild(catid);
        category.AppendChild(catname);
        root.AppendChild(category);

        //Method 2
        XmlElement category2 = xmldoc.CreateElement("Category");
        String catdata = String.Format("<CategoryId>{0}</CategoryId><CategoryName>{1}</CategoryName>", "7", "Adding data by innerXML");

        category2.InnerXml = catdata;
        root.AppendChild(category2);

        xmldoc.Save(inputfile);
    }

For further reading refer to XmlDocument and XmlNode

using System.Linq.Xml The following shall help:

static void Main(string[] args)
    {
        String inputfile = @"D:\Temp\cat.xml";

        XDocument xmldoc = XDocument.Load(inputfile);
        XElement root = xmldoc.Root;

        root.Add(new XElement("Category", new XElement("CategoryId", "8"), new XElement("CategoryName", "Added by LinqXML")));

        xmldoc.Save(inputfile);
    }

Also you can refer to this answer.

Edit: How to change the value of an element

    static void Main(string[] args)
    {
        String inputfile = @"D:\Temp\cat.xml";

        XDocument xmldoc = XDocument.Load(inputfile);
        XElement root = xmldoc.Root;
        String val = "5";

        IEnumerable<XElement> vls = from e in root.Elements("Category") where e.Element("CategoryId").Value.Equals(val) select e;

        if (vls.Count() == 1)
        {
            vls.ElementAt(0).Element("CategoryName").Value = "Value has been changed";
        }

        xmldoc.Save(inputfile);
    }

To further learn refer to this link.

Community
  • 1
  • 1
Abbas
  • 3,872
  • 6
  • 36
  • 63