snippetcsharpMinor
Convert XML to CSV
Viewed 0 times
convertcsvxml
Problem
I'm pretty sure this code can be optimized, but I'm not talented enough in Linq to do it myself. Here's what I'm trying to do: I have an XML file that needs to be converted into a .csv file. The XML looks like this:
(There are many more Items in the list, but they are all the same.)
The code I'm currently using is working as intended, here it is:
```
public void fileConvert_XMLToCSV() {
//This method converts an xml file into a .csv file
XDocument xDocument = XDocument.Load(FilePath_CSVToXML);
StringBuilder dataToBeWritten = new StringBuilder();
var results = xDocument.Descendants("Item").Select(x => new {
title = (string)x.Element("Name"),
amount = (string)x.Element("Count"),
price = (string)x.Element("Price"),
year = (string)x.Element("Year"),
productID = (string)x.Element("ProductID")
}).ToList();
for (int i = 0; i < results.Count; i++) {
string tempTitle = results[i].title;
string tempAmount = results[i].amount;
string tempPrice = results[i].price;
string tempYear = results[i].year;
string tempID = results[i].productID;
dataToBeWritten.Append(tempYear);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempTitle);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempID);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempAmount);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritt
Super Mario Bros
14
29,99
-No Comment-
N/A
Nintendo
Video Games
1985
001
The Legend of Zelda
12
34,99
-No Comment-
N/A
Nintendo
Video Games
1986
002
(There are many more Items in the list, but they are all the same.)
The code I'm currently using is working as intended, here it is:
```
public void fileConvert_XMLToCSV() {
//This method converts an xml file into a .csv file
XDocument xDocument = XDocument.Load(FilePath_CSVToXML);
StringBuilder dataToBeWritten = new StringBuilder();
var results = xDocument.Descendants("Item").Select(x => new {
title = (string)x.Element("Name"),
amount = (string)x.Element("Count"),
price = (string)x.Element("Price"),
year = (string)x.Element("Year"),
productID = (string)x.Element("ProductID")
}).ToList();
for (int i = 0; i < results.Count; i++) {
string tempTitle = results[i].title;
string tempAmount = results[i].amount;
string tempPrice = results[i].price;
string tempYear = results[i].year;
string tempID = results[i].productID;
dataToBeWritten.Append(tempYear);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempTitle);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempID);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempAmount);
dataToBeWritten.Append(";");
dataToBeWritten.Append(tempPrice);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritten.Append(";");
dataToBeWritten.Append(0);
dataToBeWritt
Solution
First of all, I'd split the convert method out into it's own thing - separate from the loading and saving:
We can simplify the actual conversion by using string interpolation and rolling it all up in a single LINQ statement:
// Load xml
XDocument xDocument = XDocument.Load(FilePath_CSVToXML);
// Convert
string data = Convert(xDocument);
// Do whatever it is you want to do with the results
Console.WriteLine(data);
Console.ReadLine();
var testpath = AppDomain.CurrentDomain.BaseDirectory + @"frMediaShop\test.csv";
File.WriteAllText(testpath, data);We can simplify the actual conversion by using string interpolation and rolling it all up in a single LINQ statement:
private static string Convert(XDocument xDocument)
{
var data = new StringBuilder();
foreach (var result in xDocument.Descendants("Item").Select(x => new {
title = (string)x.Element("Name"),
amount = (string)x.Element("Count"),
price = (string)x.Element("Price"),
year = (string)x.Element("Year"),
productID = (string)x.Element("ProductID")
}))
{
data.AppendLine($"{result.year};{result.title};{result.productID};{result.amount};{result.price};{0};{0}");
};
return data.ToString();
}Code Snippets
// Load xml
XDocument xDocument = XDocument.Load(FilePath_CSVToXML);
// Convert
string data = Convert(xDocument);
// Do whatever it is you want to do with the results
Console.WriteLine(data);
Console.ReadLine();
var testpath = AppDomain.CurrentDomain.BaseDirectory + @"frMediaShop\test.csv";
File.WriteAllText(testpath, data);private static string Convert(XDocument xDocument)
{
var data = new StringBuilder();
foreach (var result in xDocument.Descendants("Item").Select(x => new {
title = (string)x.Element("Name"),
amount = (string)x.Element("Count"),
price = (string)x.Element("Price"),
year = (string)x.Element("Year"),
productID = (string)x.Element("ProductID")
}))
{
data.AppendLine($"{result.year};{result.title};{result.productID};{result.amount};{result.price};{0};{0}");
};
return data.ToString();
}Context
StackExchange Code Review Q#159904, answer score: 9
Revisions (0)
No revisions yet.