• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3715
  • Last Modified:

C# XML to CSV File

I'm trying to convert an XML file to CSV using C# but I'm getting an empty file.

Could someone help me out? The XML file is poorly formatted but I should still be able to read the values.

This XML comes from a third party so I can do anything with the format so need to read into a csv.

XML looks like;

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
 
<string>0000256</string>
  <string>0000258</string>
  <string>0000259</string>
  <string>0000771</string>

Open in new window


My c# code;

  XElement main = XElement.Load(@"F:\******\VendaProducts.xml");

            List<string> skus = new List<string>();

            var q = from c in main.Descendants("ArrayOfString")
                    select (string)c.Element("string");


            foreach (string skuS in q)
            {
                skus.Add(skuS);
            }

            string csv = String.Join(",", skus.Select(x => x.ToString()).ToArray());

            StreamWriter writer = new StreamWriter("F:\\*******\\Products.txt", true);
            writer.WriteLine(csv);

Open in new window


Thanks
0
Gaz124
Asked:
Gaz124
1 Solution
 
Roopesh ReddyCommented:
0
 
Gaz124Author Commented:
That doesn't really help as it need an Excel instance to be opened on the server
0
 
käµfm³d 👽Commented:
How about:

XElement main = XElement.Load(@"F:\******\VendaProducts.xml");

string csvData = main.Descendants("string")
                     .Select(s => s.Value)
                     .Aggregate((accumulator, iterator) => accumulator += "," + iterator);

System.IO.File.WriteAllText("F:\\*******\\Products.txt", csvData);

Open in new window

0
 
Rose BabuSenior Team ManagerCommented:
Hi,

according to the sample xml file you have posted, check the below simple code to read xml file data into csv file.

// XMLFile3.xml
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <string>0000256</string>
  <string>0000258</string>
  <string>0000259</string>
  <string>0000771</string>
</ArrayOfString>

Open in new window

Sample Code

protected void Page_Load(object sender, EventArgs e)
{
	DataSet ds = new DataSet();
	
	// update xml file location
	ds.ReadXml(Server.MapPath("XMLFile3.xml"));

	DataTable dtString = new DataTable();
	dtString = ds.Tables[0];

	// Change csv file name or path if needed
	string strFilePath = Server.MapPath("XML2CSV.csv");

	System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false);

	// write columns
	int iCount = dtString.Columns.Count;
	for (int i = 0; i < iCount; i++)
	{
		sw.Write(dtString.Columns[i]);

		if (i < iCount - 1)
		{
			sw.Write(",");
		}
	}
	sw.Write(sw.NewLine);

	// write all the rows
	foreach (DataRow drString in dtString.Rows)
	{
		for (int i = 0; i < iCount; i++)
		{
			if (!Convert.IsDBNull(drString[i]))
			{
				sw.Write(drString[i].ToString());
			}

			if (i < iCount - 1)
			{
				sw.Write(",");
			}
		}
		sw.Write(sw.NewLine);
	}

	sw.Close();
}

Open in new window

Hope this will help you.
0
 
Gaz124Author Commented:
Spot on mate thank you
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now