Reputation: 359
Here is my sample XML,
<A>
<B id="ABC">
<C name="A" />
<C name="B" />
<C name="C" />
<C name="G" />
</B>
<B id="ZYZ">
<C name="A" />
<C name="B" />
<C name="C" />
<C name="D" />
</B>
</A>
I need to loop each of the <B>
nodes under parent <A>
and add a sequence numbered Xattribute
called Sno
to the <C>
tag for each <B>
as shown below,
<A>
<B id="ABC">
<C name="A" Sno ="1" />
<C name="B" Sno ="2"/>
<C name="C" Sno ="3"/>
<C name="G" Sno ="4"/>
</B>
<B id="ZYZ">
<C name="A" Sno ="1"/>
<C name="B" Sno ="2"/>
<C name="C" Sno ="3"/>
<C name="D" Sno ="4"/>
</B>
</A>
Using following c# code,
var final = from x in afterGrouping.Descendants("A").Descendants("B").Select((sc, i) => new {sc, sequence = i + 1})
select new XElement("C",
new XAttribute("id", x.sc.Element("C").Attribute("id").Value),
new XAttribute("sequence", x.sequence));
Upvotes: 1
Views: 456
Reputation: 54877
It might be easier to use a loop to alter the current document, rather than project one anew:
foreach (XElement b in xDoc.Descendants("B"))
{
int seq = 1;
foreach (XElement c in b.Elements("C"))
c.Add(new XAttribute("Sno", seq++));
}
Upvotes: 4
Reputation: 28059
Something like this should do the trick:
using System.Linq;
using System.Xml.Linq;
namespace ConsoleApplication19
{
class Program
{
static void Main(string[] args)
{
const string xml = @"<A><B id='ABC'><C name='A' /><C name='B' /><C name='C' /><C name='G' /></B><B id='ZYZ'><C name='A' /><C name='B' /><C name='C' /><C name='D' /></B></A>";
var doc = XDocument.Parse(xml);
var bs = doc.Descendants("B");
foreach (var r in bs)
{
var cs = r.Descendants("C");
var xElements = cs as XElement[] ?? cs.ToArray();
for (var i = 1; i <= xElements.Count(); i++)
{
var c = xElements.ElementAt(i-1);
c.SetAttributeValue("Sno", i);
}
}
var resultingXml = doc.ToString();
}
}
}
Upvotes: 0