Quantcast
Channel: User user2023861 - Code Review Stack Exchange
Viewing all articles
Browse latest Browse all 35

Answer by user2023861 for Convert XML to CSV

$
0
0

You can do it with a series of Regex.Replaces:

// 1) Replace closing and opening tags with commas.//    Include quotes in case any values have commas in them.var result = Regex.Replace(input, @"(\S)<\/[^>]*>\s*<[^>]*>(\S)", "$1\",\"$2");// 2) Put in CSV line breaks and remove xml delimiters, include leading and trailing quotesresult = Regex.Replace(result, @"<\/[^>]*>\s*<\/Item>\s*<Item>\s*<[^>]*>", "\r\n");// 3) Remove remaining tags and trim any whitespaceresult = Regex.Replace(result, @"\s*<.*>\s*", "");// 4) put in header row and first and last quotesresult = "Name,Count,Price,Comment,Artist,Publisher,Genre,Year,ProductID,\r\n\""+         result +"\"";

is this a better solution than other solutions? No. It's brittle and would break easily.

Here are the Regex steps visually:

  1. https://regex101.com/r/2jmvhc/1
  2. https://regex101.com/r/0XyvZe/1
  3. https://regex101.com/r/Sj6GsP/1

Viewing all articles
Browse latest Browse all 35

Latest Images

Trending Articles





Latest Images