Something along the following.
Solution #1
If you need to create a completely new XML file.
void Main()
{
const string SERIALS = "SERIALS";
const string POS = "POS";
const string PROCESSED = "processed";
const string FALSE = "false";
const string TRUE = "true";
int TakeElements = 2;
int posCounter = 0;
string outputXMLfile = @"C:\Share\IsDone\outputXMLfile.xml";
// just use .Load(filePath) for your real life scenario
XDocument xdoc = XDocument.Parse(@"<SERIALS>
<POS index='1' content='AAA5SUU' processed='false'/>
<POS index='2' content='AAA5SUV' processed='false'/>
<POS index='3' content='AAA5SUW' processed='false'/>
<POS index='1000' content='AAA5SVL' processed='false'/>
</SERIALS>");
posCounter = xdoc.Root.Descendants(POS)
.Where(r => r.Attribute(PROCESSED).Value.Equals(FALSE))
.Count();
if (posCounter >= TakeElements)
{
// construct new XML document
XDocument newXDoc = new XDocument(new XElement(SERIALS));
// get first TakeElements elements where processed='false'
foreach (var element in xdoc.Root.Descendants(POS)
.Where(r => r.Attribute(PROCESSED).Value.Equals(FALSE))
.Take(TakeElements))
{
// set attribute value
element.Attribute(PROCESSED).SetValue(TRUE);
// add element to the document
newXDoc.Root.Add(element);
}
// save final XML document
// uncomment when you are ready
//newXDoc.Save(outputXMLfile);
}
else
{
// do something else
}
}
Solution #2
If you need to modify the original file.
void Main()
{
const string SERIALS = "SERIALS";
const string POS = "POS";
const string PROCESSED = "processed";
const string FALSE = "false";
const string TRUE = "true";
int TakeElements = 2;
int posCounter = 0;
string outputXMLfile = @"C:\Share\IsDone\outputXMLfile.xml";
// just use .Load(filePath) for your real life scenario
XDocument xdoc = XDocument.Parse(@"<SERIALS>
<POS index='1' content='AAA5SUU' processed='false'/>
<POS index='2' content='AAA5SUV' processed='false'/>
<POS index='3' content='AAA5SUW' processed='false'/>
<POS index='1000' content='AAA5SVL' processed='false'/>
</SERIALS>");
posCounter = xdoc.Root.Descendants(POS)
.Where(r => r.Attribute(PROCESSED).Value.Equals(FALSE))
.Count();
if (posCounter >= TakeElements)
{
// get first TakeElements elements where processed='false'
foreach (var element in xdoc.Root.Descendants(POS)
.Where(r => r.Attribute(PROCESSED).Value.Equals(FALSE))
.Take(TakeElements))
{
// set attribute value
element.Attribute(PROCESSED).SetValue(TRUE);
}
// save final XML document
// uncomment when you are ready
//xdoc.Save(outputXMLfile);
}
else
{
// do something else
}
}