Thursday, October 8, 2009

Syntax for Editing Record to XML using LINQ and C#

Steps in Editing an XML using LINQ and C#

1. Get the path of your XML file.

string fullXmlPath =
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "file name.xml";

2. Load the path.

XElement LogXmlDocument = XElement.Load(fullXmlPath);

3. Prepare the data.
a. Editing an Attribure

XElement propResults = LogXmlDocument.Elements("Property").Where(
e => e.Attribute("id").Value == id).FirstOrDefault();
propResults.SetAttributeValue("Description", desc);
propResults.SetAttributeValue("Manufacturer", manufac);

b. Editing an Element

XElement propResults = LogXmlDocument.Elements("Property").Where(
e => e.Attribute("id").Value == id).FirstOrDefault();
propResults.SetElementValue("Manufacturer", manufacturer);
propResults.SetElementValue("ModelNumber", modelnumber);
propResults.SetElementValue("Type", type);

4. Save.

LogXmlDocument.Save(fullXmlPath);

No comments:

Post a Comment

About Me

My photo
simple and straight forward