Linq to XML updating a record

selecting and then updating a record in XML

Page 1 of 1

4 Replies - 13533 Views - Last Post: 23 December 2009 - 08:35 AM Rate Topic: -----

#1 DrakenKorin  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 19-February 08

Linq to XML updating a record

Posted 19 May 2008 - 01:41 AM

Hi,

Lately I've been teaching myself Linq, specifically Linq to XML, using whatever resources I can find on the interweb.

I'm having a hard time finding useful info on how to select a specific record from a xml file and then updating it.

This is how I created my XML document:

 

currentProject = new XDocument(
						new XDeclaration("1", "utf-8", "yes"));

XElement Course = new XElement("Course",
					new XElement("CourseName", courseNameXML),
				new XElement("CourseDesc", courseDescXML),
				new XElement("ReqToTrain", courseReqTrainXML),
				new XElement("CourseStatus", courseStatusXML));


and this is the resulting xml: (Well, not really, some of the contents of this XML file has been created earlier using similar code)

<Projects>
−
	<Project>
<ProjectName>TestProject 1</ProjectName>
<ProjectDesc>TestProject 1 Description</ProjectDesc>
<ProjectStart>2008/05/19 01:13:01 PM</ProjectStart>
<ProjectEnd>2008/08/21 01:13:17 PM</ProjectEnd>
</Project>
−
	<Course>
<CourseName>Test Course 1</CourseName>
<CourseDesc>Test Course 1 Description</CourseDesc>
<ReqToTrain>20</ReqToTrain>
</Course>
−
	<Course>
<CourseName>Test Course 2</CourseName>
<CourseDesc>Test Course 2 description</CourseDesc>
<ReqToTrain>10</ReqToTrain>
</Course>
−
	<Course>
<CourseName>Test Course 3</CourseName>
<CourseDesc>Test Course 3 Description</CourseDesc>
<ReqToTrain>5</ReqToTrain>
</Course>
−
	<Venue>
<VenueName>Test Venue 1</VenueName>
<VenueCap>20</VenueCap>
</Venue>
−
	<Venue>
<VenueName>Test Venue 2</VenueName>
<VenueCap>10</VenueCap>
</Venue>
−
	<Venue>
<VenueName>Test Venue 3</VenueName>
<VenueCap>5</VenueCap>
</Venue>
</Projects>


I'm having a hard time figuring out how to select a specific <course> node, and its corresponding <ReqToTrain> node.

Any help will be greatly appreciated!

This is for a training logistics app. Basically it allows you to setup various training courses, the number of people required to train and course duration, then matches those training courses to training venues. The app divides the number of people requiring training with the selected venue capacity and works out how many days you require to complete your training course. After everything is setup, you can export your training schedule to Outlook.

Help me with this one little issue, and I will gladly post this entire app and it's source code as a tutorial here on DIC

"Edit"
Oops! I forgot to add, that at this stage, the XML document does not exists anywhere on the local file system, it is currently only in memory, and only gets created when the user decides to save the current "project"

This post has been edited by DrakenKorin: 19 May 2008 - 02:23 AM


Is This A Good Question/Topic? 1
  • +

Replies To: Linq to XML updating a record

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Linq to XML updating a record

Posted 23 December 2009 - 08:12 AM

Where you able to figure this problem out? I am having a similar problem.
Was This Post Helpful? 0
  • +
  • -

#3 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Linq to XML updating a record

Posted 23 December 2009 - 08:22 AM

Searching for "linqtoxml update record" via Google yielded this in the first hit. Is that helpful? I've not had a need for LINQ yet.
Was This Post Helpful? 0
  • +
  • -

#4 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Linq to XML updating a record

Posted 23 December 2009 - 08:26 AM

Once again your prowess with Google amazes me. That was the perfect answer thanks. FYI I did google, but had a different wording with no useful results.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: Linq to XML updating a record

Posted 23 December 2009 - 08:35 AM

here is the XML file that I used...

<?xml version="1.0" encoding="utf-8"?>
<Projects>
    <Project>
		<ProjectName>TestProject 1</ProjectName>
		<ProjectDesc>TestProject 1 Description</ProjectDesc>
		<ProjectStart>2008/05/19 01:13:01 PM</ProjectStart>
		<ProjectEnd>2008/08/21 01:13:17 PM</ProjectEnd>
	</Project>
    <Course>
		<CourseName>Test Course 1</CourseName>
		<CourseDesc>Test Course 1 Description</CourseDesc>
		<ReqToTrain>20</ReqToTrain>
	</Course>
    <Course>
		<CourseName>Test Course 2</CourseName>
		<CourseDesc>Test Course 2 description</CourseDesc>
		<ReqToTrain>10</ReqToTrain>
	</Course>
    <Course>
		<CourseName>Test Course 3</CourseName>
		<CourseDesc>Test Course 3 Description</CourseDesc>
		<ReqToTrain>5</ReqToTrain>
	</Course>
    <Venue>
		<VenueName>Test Venue 1</VenueName>
		<VenueCap>20</VenueCap>
	</Venue>
    <Venue>
		<VenueName>Test Venue 2</VenueName>
		<VenueCap>10</VenueCap>
	</Venue>
    <Venue>
		<VenueName>Test Venue 3</VenueName>
		<VenueCap>5</VenueCap>
	</Venue>
</Projects>



here is the code I used....

private int GetReqToTrain(string courseName)
{
    XDocument doc = Xdocument.Load(@"C:\test\test.xml");

    var reqToTrain = from c in doc.Root.Descendants("Course")
                     where c.Element("CourseName").Value == courseName
                     select c.Element("ReqToTrain").Value;

    return int.Parse(reqToTrain.First());
}



and to use it..

private void button1_Click(object sender, EventArgs e)
{
    int value = GetReqToTrain("Test Course 2");
    MessageBox.Show("value = " + value.ToString());
}




Then to Update the value....

private void Update(string courseName, int newValue)
{
    XDocument doc = Xdocument.Load(@"C:\test\test.xml");

    var reqToTrain = from c in doc.Root.Descendants("Course")
                     where c.Element("CourseName").Value == courseName
                     select c;

    XElement element = reqToTrain.First();
    element.SetElementValue("ReqToTrain", newValue);
    doc.Save(@"C:\test\test.xml");
}



and to use...

private void button_Click(object sender, EventArgs e)
{
    Update("Test Course 2", 25);
}


This post has been edited by eclipsed4utoo: 23 December 2009 - 08:49 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1