tee Eff

tee Eff

  • NA
  • 1
  • 4.3k

Xelement and LINQ in C#

Jun 7 2012 3:29 PM
Iterate through a document tree in XML and LINQ

foreach
(var c in childList)
if (c.Name.LocalName.Equals(PNODES.CHILDdata))
{
Exel = c
as XElement; //store the current node
XAttribute asDate = c.Attribute("AssessmentDate");
XAttribute asGuid = c.Attribute("AssessmentGUID");
XAttribute asType = c.Attribute("AssessmentType");
XAttribute asOper = c.Attribute("Operation");
XAttribute asPartnerGuid = c.Attribute("PartnerGUID");
}
int level = 1; //a way to determine if we are on first node (record) of CHILD_PAF
XElement tree = document;
//DeepCopy the element and set its Properties
foreach (var t in tbl)
{
XElement newXl = new XElement(Exel); //deep copy the element
newXl.Attribute(
"AssessmentDate").Value = t.AssessmentDate.ToString();
newXl.Attribute(
"AssessmentGUID").Value = t.AssessmentGUID.ToString();
newXl.Attribute(
"AssessmentType").Value = t.AssessmentType.ToString();
newXl.Attribute(
"Operation").Value = t.operation.ToString();
newXl.Attribute(
"PartnerGUID").Value = t.PartnerGUID.ToString();
//Set the CurrentAssessmentID so other functions know which record we are working with
_CurrentAssessmentID = t.AssessmentID;
if (level == 1)
{
Exel.ReplaceWith(newXl);
//replace the first element with the updated copy
level = 2;
// tree.Save("tree.xml"); //debug to examine the contents
}
else
{
tree.Add(newXl);
}
}
/*Persist the data in case we want to look at it later.*/
tree.Save(
"CHILDData.xml");