Came across a situation today where XML file has to be read, Search for all the occurrences of a element, Get the value of the searched element concatenated with comma separator. A simple, couple of lines, of code using LINQ does the trick. Here it is
string xml = File.ReadAllText("Input File Name");
XElement element = XElement.Parse(xml);
var soNumber = element.Descendants()
.Where(ele => ele.Name.LocalName.Contains("Element To Search For"))
.Select(ele => ele.Value)
.Distinct()
.Aggregate(new StringBuilder(), (current, next) => current.Append("'" + next + "',"));
No comments:
Post a Comment