I have an XML which look like this,
<Names>
<Name>
<FName>Abc</FName>
<LName>XYZ</LName>
</Name>
<Name>
<FName>Abc2</FName>
<LName>XYZ2</LName>
</Name>
<Name>
<FName>Abc3</FName>
<LName>XYZ3</LName>
</Name>
<Name>
<FName>Abc4</FName>
<LName>XYZ4</LName>
</Name>
</Names>
I am trying to change the Node names prophetically in c# if their occurrence is more than once and Except the first node. First node name remains same;
after processing XML should look like this;
<Names>
<Name> // kepp first node name same
<FName>Abc</FName>
<LName>XYZ</LName>
</Name>
<ChildName> //changed
<FName>Abc2</FName>
<LName>XYZ2</LName>
</ChildName>
<ChildName> //changed
<FName>Abc3</FName>
<LName>XYZ3</LName>
</ChildName>
<ChildName> // changed
<FName>Abc4</FName>
<LName>XYZ4</LName>
</ChildName>
</Names>
Node of Name can be one, if it is once, keep the same, if more than leave the first and change the names of others.
I am trying like this XML object is of IEnumerable<XElement> type;
//check if NAME node occurs multiple times, make other to child.
var nameCounts = element.Descendants().Where(x => x.Name.LocalName == "Name");
int number = nameCounts.Count();
if (number > 1) // if occurance more than one
{
foreach (var e in element.Descendants().Where(x => x.Name.LocalName == "Name").ToList())
{
//e.NodesAfterSelf();
// unable to understand what to do
}
}
UPDATE:
However I got the answer of my question; I made a little mistake actually my XML look like this;
<ListOfNames>
<Names>
<Name> // two occurrence, 2nd will change to CHILD NAME
<FName>Abc</FName>
<LName>XYZ</LName>
</Name>
<Name>
<FName>Abc2</FName>
<LName>XYZ2</LName>
</Name>
</Names>
<Names> // three occurrence, 2nd and 3rd will change to CHILDNAME
<Name>
<FName>Abc</FName>
<LName>XYZ</LName>
</Name>
<Name>
<FName>Abc2</FName>
<LName>XYZ2</LName>
</Name>
<Name>
<FName>Abc2</FName>
<LName>XYZ2</LName>
</Name>
</Names>
<Names> // only one occurrence, nothing change
<Name>
<FName>Abc</FName>
<LName>XYZ</LName>
</Name>
</Names>
</ListOfNames>
<ListOfNames>as parent node it has several<Names>nodes containing values as written, then how would I change node names in all<Names>except first occurrence of<Name>