samedi 25 avril 2015

How to Updating XML File (C#/Linq) in Windows Form


4 down vote favorite 1

Hello Friend 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.

thanks


Aucun commentaire:

Enregistrer un commentaire