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. XML Mapping to CSV

XML Mapping to CSV

Scheduled Pinned Locked Moved C#
xml
10 Posts 4 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.
  • R Offline
    R Offline
    RickSharp
    wrote on last edited by
    #1

    Hello, I have a fixed XML table thats coming from a Web Service.

    JOHNSON, DANIEL R.
    BMRES-550
    Bank of America
    123456
    49,7560.8600

    I need to convert the XML data to CSV. The data to be converted has commas. So the below method doesn't work:

    using (StreamWriter writer = File.CreateText("C:\\username\\password\\Desktop\\Export.csv"))
    {
    DataSet ds = new DataSet();
    ds.ReadXml(XmlReader.Create(new StringReader(myWebreference.GetWebServiceMethod("username", "password1", "Export", "Parameter4", DateTime.Today, DateTime.Today, ""))));

            writer.WriteLine(string.Join(",", ds.Tables\["Loan"\].Columns.Cast().Select(col => col.ColumnName).ToArray()));
            foreach (DataRow row in ds.Tables\["Loan"\].Rows)
            {
                writer.WriteLine(string.Join(",", row.ItemArray.Cast().ToArray()));
    
            }
        }
    
    D P J 3 Replies Last reply
    0
    • R RickSharp

      Hello, I have a fixed XML table thats coming from a Web Service.

      JOHNSON, DANIEL R.
      BMRES-550
      Bank of America
      123456
      49,7560.8600

      I need to convert the XML data to CSV. The data to be converted has commas. So the below method doesn't work:

      using (StreamWriter writer = File.CreateText("C:\\username\\password\\Desktop\\Export.csv"))
      {
      DataSet ds = new DataSet();
      ds.ReadXml(XmlReader.Create(new StringReader(myWebreference.GetWebServiceMethod("username", "password1", "Export", "Parameter4", DateTime.Today, DateTime.Today, ""))));

              writer.WriteLine(string.Join(",", ds.Tables\["Loan"\].Columns.Cast().Select(col => col.ColumnName).ToArray()));
              foreach (DataRow row in ds.Tables\["Loan"\].Rows)
              {
                  writer.WriteLine(string.Join(",", row.ItemArray.Cast().ToArray()));
      
              }
          }
      
      D Offline
      D Offline
      David C Hobbyist
      wrote on last edited by
      #2

      Why not convert comma's to something that wont be in your data like ~ ?

      Frazzle the name say's it all
      Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. John F. Woods

      R 1 Reply Last reply
      0
      • D David C Hobbyist

        Why not convert comma's to something that wont be in your data like ~ ?

        Frazzle the name say's it all
        Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. John F. Woods

        R Offline
        R Offline
        RickSharp
        wrote on last edited by
        #3

        Actually this might be more what I'm looking for:

        public class WireEntry
        {
        public String Name { get; set; }
        public String Number { get; set; }
        public String BankName { get; set; }
        public String ClosingNo { get; set; }

        public WireEntry()
        {
        }
        

        }

        foreach (DataRow row in ds.Tables["Loan"].Rows)
        {

                // Enter into result set
        
                WireEntry newEntry = new WireEntry();
                newEntry.Name = row\["Name"\].ToString();
                newEntry.Number = row\["Number"\].ToString();
                newEntry.BankName = row\["BankName"\].ToString();
        
                result.Add(newEntry);
        
            }
        
            return result;
        
        D 1 Reply Last reply
        0
        • R RickSharp

          Actually this might be more what I'm looking for:

          public class WireEntry
          {
          public String Name { get; set; }
          public String Number { get; set; }
          public String BankName { get; set; }
          public String ClosingNo { get; set; }

          public WireEntry()
          {
          }
          

          }

          foreach (DataRow row in ds.Tables["Loan"].Rows)
          {

                  // Enter into result set
          
                  WireEntry newEntry = new WireEntry();
                  newEntry.Name = row\["Name"\].ToString();
                  newEntry.Number = row\["Number"\].ToString();
                  newEntry.BankName = row\["BankName"\].ToString();
          
                  result.Add(newEntry);
          
              }
          
              return result;
          
          D Offline
          D Offline
          David C Hobbyist
          wrote on last edited by
          #4

          Maybe I am wrong but you still need to strip out the comma's from the incoming data. Or possibly I didn't understand your task. I was thinking string.Replace(",","comma");

          Frazzle the name say's it all
          Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. John F. Woods

          1 Reply Last reply
          0
          • R RickSharp

            Hello, I have a fixed XML table thats coming from a Web Service.

            JOHNSON, DANIEL R.
            BMRES-550
            Bank of America
            123456
            49,7560.8600

            I need to convert the XML data to CSV. The data to be converted has commas. So the below method doesn't work:

            using (StreamWriter writer = File.CreateText("C:\\username\\password\\Desktop\\Export.csv"))
            {
            DataSet ds = new DataSet();
            ds.ReadXml(XmlReader.Create(new StringReader(myWebreference.GetWebServiceMethod("username", "password1", "Export", "Parameter4", DateTime.Today, DateTime.Today, ""))));

                    writer.WriteLine(string.Join(",", ds.Tables\["Loan"\].Columns.Cast().Select(col => col.ColumnName).ToArray()));
                    foreach (DataRow row in ds.Tables\["Loan"\].Rows)
                    {
                        writer.WriteLine(string.Join(",", row.ItemArray.Cast().ToArray()));
            
                    }
                }
            
            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            What do you do with the CSV when you're done? Why use CSV? 0) Use a different delimiter (Good). 1) Wrap the values in quotes (better). 2) I'd prefer to save the XML and then use XSLT to form the CSV.

            R 1 Reply Last reply
            0
            • P PIEBALDconsult

              What do you do with the CSV when you're done? Why use CSV? 0) Use a different delimiter (Good). 1) Wrap the values in quotes (better). 2) I'd prefer to save the XML and then use XSLT to form the CSV.

              R Offline
              R Offline
              RickSharp
              wrote on last edited by
              #6

              Actually thats a good question. Because I'm not really wanting the end file to be a CSV file. The end file should be in FedWire Format. http://www.frbservices.org/campaigns/remittance/files/fedwire_funds_format_reference_guide.pdf[^] I just figured it would be easier to get to my end result with a CSV file to work with. Basically I'm taking that Web Service. Iterating through the tables. Then ultimately spitting out a file in that format. {1015} blah blah blah {1870} blah blah blah

              P J 2 Replies Last reply
              0
              • R RickSharp

                Actually thats a good question. Because I'm not really wanting the end file to be a CSV file. The end file should be in FedWire Format. http://www.frbservices.org/campaigns/remittance/files/fedwire_funds_format_reference_guide.pdf[^] I just figured it would be easier to get to my end result with a CSV file to work with. Basically I'm taking that Web Service. Iterating through the tables. Then ultimately spitting out a file in that format. {1015} blah blah blah {1870} blah blah blah

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

                Cut out the middlemen (including the DataSet). Look into XSLT to transform the XML to FedWire directly. I can probably help, but I won't read that document until I get home.

                R 1 Reply Last reply
                0
                • P PIEBALDconsult

                  Cut out the middlemen (including the DataSet). Look into XSLT to transform the XML to FedWire directly. I can probably help, but I won't read that document until I get home.

                  R Offline
                  R Offline
                  RickSharp
                  wrote on last edited by
                  #8

                  Hmmm Ok. Thanks for the brainstorming. Guess i dont need a dataset at all if XLST can give me both displaying the data in a meaningful manner front end while also being able to convert the data and spitting it out to a file, which it sounds like it can. I look into it until you get back to me. :)

                  1 Reply Last reply
                  0
                  • R RickSharp

                    Hello, I have a fixed XML table thats coming from a Web Service.

                    JOHNSON, DANIEL R.
                    BMRES-550
                    Bank of America
                    123456
                    49,7560.8600

                    I need to convert the XML data to CSV. The data to be converted has commas. So the below method doesn't work:

                    using (StreamWriter writer = File.CreateText("C:\\username\\password\\Desktop\\Export.csv"))
                    {
                    DataSet ds = new DataSet();
                    ds.ReadXml(XmlReader.Create(new StringReader(myWebreference.GetWebServiceMethod("username", "password1", "Export", "Parameter4", DateTime.Today, DateTime.Today, ""))));

                            writer.WriteLine(string.Join(",", ds.Tables\["Loan"\].Columns.Cast().Select(col => col.ColumnName).ToArray()));
                            foreach (DataRow row in ds.Tables\["Loan"\].Rows)
                            {
                                writer.WriteLine(string.Join(",", row.ItemArray.Cast().ToArray()));
                    
                            }
                        }
                    
                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Normal form for "CSV" is to put double quotes around values that have commas. With double quotes the value is also surrounded by double quotes and each double quote in it is then escaped with two double quotes.

                    1 Reply Last reply
                    0
                    • R RickSharp

                      Actually thats a good question. Because I'm not really wanting the end file to be a CSV file. The end file should be in FedWire Format. http://www.frbservices.org/campaigns/remittance/files/fedwire_funds_format_reference_guide.pdf[^] I just figured it would be easier to get to my end result with a CSV file to work with. Basically I'm taking that Web Service. Iterating through the tables. Then ultimately spitting out a file in that format. {1015} blah blah blah {1870} blah blah blah

                      J Offline
                      J Offline
                      jschell
                      wrote on last edited by
                      #10

                      RickSharp wrote:

                      The end file should be in FedWire Format.

                      The posted link is not a Protocol structure document. It looks like a mapping document.

                      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