Linq to xml. Determine the end of the tag



I have a piece of xml like the following:



<side>
<category>
<title>Head</head>
<item> Mister a </item>
</category>
<category>
<title>Head</head>
<item> Mister x </item>
<item> Mister y </item>
</category>
</side>


I am recovering for each category title and item and once arrived at the end of the category, place a separator that allows to separate categories. For now I can recover well for each category title and item, but I can not place a separator between two categories


this is my code in C# :



foreach (XElement node in doc.Descendants("category").Elements())
{

if (node.Name.LocalName.Equals("titre"))
{
layout.Records.Add(new XMLRecord()
{
Type = "Titre",
Contenu = node.Value
});

}
else if (node.Name.LocalName.Equals("item"))
{
layout.Records.Add(new XMLRecord()
{
Type = "Item",
Contenu = node.Value
});

}
else if (node.Name.LocalName.Equals("item") && node.NextNode == null)
{
layout.Records.Add(new XMLRecord()
{
Type = "Item",
Contenu = node.Value
});

layout.Records.Add(new XMLRecord()
{
Type = "Separateur",
Contenu = ""
});


}




}
return layout.Records;

No comments:

Post a Comment