I have xml in format below:
<rows>
<rows>
<primitiveValue>000</primitiveValue>
</rows>
<rows>
<primitiveValue>58316</primitiveValue>
</rows>
<rows>
<primitiveValue>133083.0</primitiveValue>
</rows>
</rows>
<rows>
<rows>
<primitiveValue>001</primitiveValue>
</rows>
<rows>
<primitiveValue>66018</primitiveValue>
</rows>
<rows>
<primitiveValue>172546.0</primitiveValue>
</rows>
</rows>
I need to split the data per parent element of rows, but don’t know how to do this.
I’ve tried the code below, but this loops through every instance of rows and causes duplication.
string Values = "";
foreach (XElement element in xDoc.Descendants("rows"))
{
foreach (XElement childElement in element.Descendants("rows").Elements("primitiveValue"))
{
Values = Values + "," + childElement.Value.ToString();
}
}
Can someone help please?
Very simple with
System.Xml.XPath.Extensions:Values will be
IEnumerable<double>containing values of primitiveValue elements. And result will be concatenated string of all values. BTW you are missing root element in your xml.UPDATE
This will return sequence of strings, each of string will contain concatenated values of inner rows. If you need raw strings instead of double values, then cast value nodes to string.