Trying to get values of some child elements in XML, however, when retrieving the values of these elements, they are stitched together.
Also, sometimes the file can have 1 reference number but there can also be multiple.
Code below
public class Test
{
public static void ParseXml(string xml)
{
var doc = XDocument.Parse(xml);
List<KeyValuePair<string, string>> caseList = new List<KeyValuePair<string, string>>();
var t = 1;
foreach (var el in doc.Descendants("ReferenceNumbers"))
{
//Console.WriteLine(el.ToString());
caseList.Insert(0, new KeyValuePair<string, string>(t.ToString(), el.Value));
t++;
}
for (int x = 0; x < caseList.Count; x++)
{
Console.WriteLine(caseList[x]);
}
}
public static void Main()
{
ParseXml(@"<root>
<ReferenceNumbers>
<Reference1>CN534786</Reference1>
<Reference2>CN476587</Reference2>
</ReferenceNumbers>
</root>");
}
}
and the result is
[1, CN534786CN476587]
However, I'm expecting [1, CN534786] [2, CN476587]