The following snippet converts xml data to csv data in a data processing application. element
is a XElement
. I'm currently trying to optimize the performance of the application and was wondering if I could somehow combine the two operations going on below: Ultimately I still want access to the string value of joined and the elements of values in a list because they are used later on for other purposes. Not sure if this is doable. Any help will be appreciated!
The first operation basically strips the XML data of all tags and just returns the text data between them or outside them. It also checks the formatting. The second operation takes the XML data and removes all line breaks and spaces if they exist within the first few characters of the data.
IEnumerable<string> values = new List<string>();
values = element.DescendantNodes().OfType<XText>()
.Select(v => Regex.Replace(v.Value, "\\s+", " ")).ToList();
string joined = string.Concat(element.ToString().Split().Take(3)) + string.Join(" ", element.
ToString().Split().Skip(3));