1

Hi I have the following edited application code -

 public partial class ConvertApp : Form

 {

     public ConvertApp()
    {
        InitializeComponent();  
   }
    static webservice.Results res= new webservice.Results(out int factSent,out int ComSent)
    private static webservice.Results convert()
    {
        string csv = File.ReadAllText("C:\\test.txt");
        string year = "1000" ;
        XDocument doc = ConvertCsvToXML(csv, new[] { "," });

        webservice.Feed CallWebService = new webservice.Feed();

    int recordAmount=0; 
        factSent = 0;
        ComSent = 0;
        foreach(XElement el in doc.Descendants("row"))
        {
             recordAmount = recordAmount+1;
        } 
        webservice.Summary[] Summary = new webservice.Summary[recordAmount];
        int i=0;

        foreach(XElement el in doc.Descendants("row"))
        {
            Summary[i] = new webservice.Summary();
            Summary[i].person = el.Descendants("var").Where(x => (string)x.Attribute("name") == "person").SingleOrDefault().Attribute("value").Value;
            Summary[i].fact = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "fact").SingleOrDefault().Attribute("value").Value);
            Summary[i].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value);
            Summary[i].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value;
            Summary[i].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value;
        factSent += Summary[i].fact;    
        ComSent +=Summary[i].com;
        i=i+1;
        }
        webservice.Results res = CallWebService.updateStatus(Summary, year);
        return res;
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        int factSent;
        int ComSent;
        webservice.Results returned = convert(out factSent, out ComSent);
        txtResult.Text = System.Convert.ToString(returned.status);
        txtMoreRes.Text = returned.errorDetails[i].errorDetails;
        factSum.Text = System.Convert.ToString(returned.factSum);
        comSum.Text = System.Convert.ToString(returned.comSum);
        factSumSent.Text = factSent.ToString();
        comSumSent.Text = ComSent.ToString();
        time.Text = System.Convert.ToString(DateTime.Now.TimeOfDay);
        date.Text = System.Convert.ToString(DateTime.Today);     
        lineFault.Text = System.Convert.ToString(returned.errDetails[i].lineFault);
       }

My problem is I used the method of gettng the results to load into a web form for demonstration purposes. However now I want to get the xml response to load into a CSV file and save that file automatically to a specific path, I no longer need the application to be a web form, and not sure of how to do this.

Here is an example of the response XML -

<return>
                    <factSum>24</factSum>
                    <comSum>15</comSum>
                    <errDetails>
                          <errors>The person [123] is unknown , The
                                centre [14] is unknown </errors>
                          <Position>0</Position>
                    </errDetails>
                    <status>lineFault</status>
              </return>
    </ns2:FeedResponse>
</S:Body>

And I want the .CSV file to look something like-

date,time,factSent,ComSent,factSum,comSum,errors,Position,status 26/01/2011,14:00,24,15,24,15,The person[123] is unknown,0,lineFault

I have tried StringBuilder SbCSV but could not get it to work as I was not sure how to alter my existing code to use it. I also tried using an XLS template using System.Configuration; and System.Xml.Xsl; that looked like -

<xsl:template match="//return".
<xsl:value-of select="factsum">
//etc
</xsl:template>

And then took out the - private void Form1_Load(object sender, EventArgs e) and entered

static void Main(string[] args)
{
   string oldXML = ConfigurationSettings.AppSettings["res"];
   string xsltLocation = ConfigurationSettings.AppSettings["Path to where xsl template is saved"];
   string new CSV = ConfigurationSettings.AppSettings["Path to where I want the new CSV to go"];

   XslCompiledTransform transform = new XslCompiledTransform();
   transform.Load("Path to where xsl template is saved");
   transform.Transform(oldXML, newCSV);
}

This however gave me problems with the public partial class ConvertApp : Form and also that it was a static void Main and I think the template was configured incorrectly.

Any help would be GREATLY appreciated and thanks for your time.

eftpotrm
  • 2,221
  • 1
  • 19
  • 23
Ebikeneser
  • 2,530
  • 12
  • 48
  • 101

2 Answers2

2

This was discussed in chat, with the following conclusions:

  • the objective is to write a file response from a web form, as covered here
  • the translation of the input xml to the target csv is pretty much covered, via xslt (as it happens, pretty much like here)
  • and to write csv, we want to set the "content-type" and "content-dispositon" headers, as discussed here
Community
  • 1
  • 1
Marc Gravell
  • 927,783
  • 236
  • 2,422
  • 2,784
0

I'm not entirely sure I follow the problem you're having but might it be easier to create a new project from the appropriate template and paste in the relevant stringbuilder-based code than trying to amend your web service project?

eftpotrm
  • 2,221
  • 1
  • 19
  • 23
  • i just need to get the results to save in csv format then save to a specific location but still racking my brains! – Ebikeneser Jan 26 '11 at 14:51
  • Parsing your input and creating a StringBuilder as you go is the right way to build a CSV version, IMHO, and simple to save out to disk at the end. I'm still not quite sure I follow the exact problem but it appears to be something to do with converting the project from outputting as a web service to producing this text file. In which case perhaps a new project using a more appropriate base template (windows / console app for example) and pasting the relevant code into that might make your life simpler. – eftpotrm Jan 26 '11 at 15:47