Can't figure out how to replace or add an XML element using LINQ

dwightblue

New Member
okay so im making a program where you pick the current day, and add things that you ate that da,y and it logs it into an xml file. you choose when you ate also such as breakfast-postworkout ect. now my xml looks like this\[code\]<?xml version="1.0" encoding="utf-8" ?><Log> <DAY date="11/8/1995" id ="1"> <Breakfast> <food size="1">mys</food> </Breakfast> <MidAfternoon> <food size="2">mys</food> </MidAfternoon> <Lunch> <food size="0.5">mys</food> </Lunch> <PostWorkout> <food size="10">mys</food> </PostWorkout> <BeforeBed> <food size="20">mys</food> </BeforeBed> </DAY> <DAY date="5/20/12" id="2"> <Breakfast> <food size="1">mys</food> </Breakfast> <MidAfternoon> <food size="2">mys</food> </MidAfternoon> <Lunch> <food size="0.5">mys</food> </Lunch> <PostWorkout> <food size="10">mys</food> </PostWorkout> <BeforeBed> <food size="20">mys</food> </BeforeBed> </DAY>\[/code\]what i need to do is 1 find out if the date exists or not if not make a new "DAY"(i know how to do this) but if it searches the xml file and finds that the current picked date(using date picker) already has a DAY recorded to figure out which time the user ate this food and add it into that sectionwe know when the user ate the food by a combobox that they select the time basically what i need help with is how do narrow it down to finding the correct "DAY" by using that "DAY"'s date attribute then with the search narrowed find the correct time of day within the "DAY" to place the new \[code\]<food size="numberhere">foodname</food>\[/code\]please help all i've gotten so far is\[code\] var NOD = from dates in xmlDoc.Descendants("DAY") select dates; int i = 0; foreach (XElement d in NOD) { dlist = d; i++; } for (int j = 0; j < i; j++ ) { if (dlist[j].Attribute("date").Value =http://stackoverflow.com/questions/10679348/= datePicker1.Text) { Console.WriteLine("this date already has a record"); bRecorded = true; break; } bRecorded = false; }\[/code\]also if anyone knows a better way of doing the above that would be great thank you very much.
 
Back
Top