Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. creating csv from xml file in c#

creating csv from xml file in c#

Scheduled Pinned Locked Moved C#
csharpxmlquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    laziale
    wrote on last edited by
    #1

    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

    F P 2 Replies Last reply
    0
    • L laziale

      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

      F Offline
      F Offline
      Fayu
      wrote on last edited by
      #2

      post the xml

      L 1 Reply Last reply
      0
      • F Fayu

        post the xml

        L Offline
        L Offline
        laziale
        wrote on last edited by
        #3

        <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

        F 1 Reply Last reply
        0
        • L 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

          F Offline
          F Offline
          Fayu
          wrote on last edited by
          #4

          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));
                          }
                      }
          
          L 1 Reply Last reply
          0
          • F Fayu

            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));
                            }
                        }
            
            L Offline
            L Offline
            laziale
            wrote on last edited by
            #5

            thanks, that really helped me. have a nice day, Laziale

            1 Reply Last reply
            0
            • L laziale

              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

              P Offline
              P Offline
              PIEBALDconsult
              wrote on last edited by
              #6

              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?

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups