creating csv from xml file in c#
-
hello guys, I want to create a csv file from xml document, can someone please point me on some article to see how is this done? Thanks in advance, Laziale
-
<restaurants>
<Business name="someBusiness">
<Address>
<Street>someStreet</Street>
<City>Las Vegas</City>
<Zip>89169</Zip>
<State>NV</State>
</Address>
<Contact>
<ContactPerson>someCP</ContactPerson>
<PhoneNumber>(702) 889-5587</PhoneNumber>
<MantaWebsite>http://www.sdadas.com</MantaWebsite>
</Contact>
</Business>
</restaurants>thanks for helping me
-
<restaurants>
<Business name="someBusiness">
<Address>
<Street>someStreet</Street>
<City>Las Vegas</City>
<Zip>89169</Zip>
<State>NV</State>
</Address>
<Contact>
<ContactPerson>someCP</ContactPerson>
<PhoneNumber>(702) 889-5587</PhoneNumber>
<MantaWebsite>http://www.sdadas.com</MantaWebsite>
</Contact>
</Business>
</restaurants>thanks for helping me
The easiest way is to dump the xml into a dataset and enumerate throught the rows and write to file. Look at the code below.
public static void SaveXmlToFile(string xml, string output) { string seperator = ";"; DataSet ds = new DataSet(); DataTable bzTable = null; DataTable adTable = null; DataTable cntTable = null; StringBuilder sb = new StringBuilder(); //Dump xml into dataset using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sw = new StreamWriter(ms)) { sw.BaseStream.Seek(0, SeekOrigin.Begin); sw.Write(xml); sw.Flush(); sw.BaseStream.Seek(0, SeekOrigin.Begin); ds.ReadXml(sw.BaseStream); } } //Set tables after ds has been built bzTable = ds.Tables\["Business"\]; adTable = ds.Tables\["Address"\]; cntTable = ds.Tables\["Contact"\]; //Read Business table foreach (DataRow bzRow in bzTable.Rows) { //Get Business\_id; Generated when dumping data into ds int curBzId = Convert.ToInt32(bzRow\["Business\_Id"\]); //Enumerate though business tables and write data into string builder foreach (DataColumn bzCol in bzTable.Columns) { sb.Append(string.Format("{0}{1}", bzRow\[bzCol\].ToString(), seperator)); } //Filter Address column for curBzId and write values to string builder foreach (DataRow adRow in adTable.Select(string.Format("\[Business\_Id\]={0}", curBzId))) { foreach (DataColumn adCol in adTable.Columns) { sb.Append(string.Format("{0}{1}", adRow\[adCol\].ToString(), seperator)); } } //Filter Contact column for curBzId and write values to string builder foreach (DataRow cntRow in cntTable.Select(string.Format("\[Business\_Id\]={0}", curBzId))) { foreach (DataColumn cntCol in cntTable.Columns) { sb.Append(string.Format("{0}{1}", cntRow\[cntCol\].ToString(), seperator)); } }
-
The easiest way is to dump the xml into a dataset and enumerate throught the rows and write to file. Look at the code below.
public static void SaveXmlToFile(string xml, string output) { string seperator = ";"; DataSet ds = new DataSet(); DataTable bzTable = null; DataTable adTable = null; DataTable cntTable = null; StringBuilder sb = new StringBuilder(); //Dump xml into dataset using (MemoryStream ms = new MemoryStream()) { using (StreamWriter sw = new StreamWriter(ms)) { sw.BaseStream.Seek(0, SeekOrigin.Begin); sw.Write(xml); sw.Flush(); sw.BaseStream.Seek(0, SeekOrigin.Begin); ds.ReadXml(sw.BaseStream); } } //Set tables after ds has been built bzTable = ds.Tables\["Business"\]; adTable = ds.Tables\["Address"\]; cntTable = ds.Tables\["Contact"\]; //Read Business table foreach (DataRow bzRow in bzTable.Rows) { //Get Business\_id; Generated when dumping data into ds int curBzId = Convert.ToInt32(bzRow\["Business\_Id"\]); //Enumerate though business tables and write data into string builder foreach (DataColumn bzCol in bzTable.Columns) { sb.Append(string.Format("{0}{1}", bzRow\[bzCol\].ToString(), seperator)); } //Filter Address column for curBzId and write values to string builder foreach (DataRow adRow in adTable.Select(string.Format("\[Business\_Id\]={0}", curBzId))) { foreach (DataColumn adCol in adTable.Columns) { sb.Append(string.Format("{0}{1}", adRow\[adCol\].ToString(), seperator)); } } //Filter Contact column for curBzId and write values to string builder foreach (DataRow cntRow in cntTable.Select(string.Format("\[Business\_Id\]={0}", curBzId))) { foreach (DataColumn cntCol in cntTable.Columns) { sb.Append(string.Format("{0}{1}", cntRow\[cntCol\].ToString(), seperator)); } }
-
hello guys, I want to create a csv file from xml document, can someone please point me on some article to see how is this done? Thanks in advance, Laziale
I have done that with XSLT, but it's tricky. I see you posted a snippet of XML, could you also post how you want the CSV to look?