views:

226

answers:

2

I have a collection of strings that are XML content. I want to iterate thru my collection and build a CSV file to stream to the user for download (sometimes it can be hundreds in the collection). This is my loop:

foreach (string response in items.Responses)
{
    string xmlResponse = response;

    //BUILD CSV HERE
}

This is what my XML content looks like for each iteration (xmlResponse). I want to put it in a flat file including the "properties" attributes:

<?xml version="1.0"?>
<response>
  <properties id="60375c90-9dd7-400f-aafb-a8726df409a9" name="Account Request" date="Thursday, March 04, 2010 2:14:07 PM" page="http://mydomain/sitefinity/CreateAccount.aspx" ip="192.168.1.255" browser="Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.8) Gecko/20100202 Firefox/3.5.8" referrer="http://mydomain/sitefinity/CreateAccount.aspx" confirmation="True" subject="Email from website: Account Request Form" sender="[email protected]" recipients="[email protected], , " />
  <fields>
    <field>
      <label>Personal Details</label>
      <value>Personal Details</value>
    </field>
    <field>
      <label>Name</label>
      <value>Tim Wales</value>
    </field>
    <field>
      <label>Email</label>
      <value>[email protected]</value>
    </field>
    <field>
      <label>Website</label>
      <value></value>
    </field>
    <field>
      <label>Password</label>
      <value></value>
    </field>
    <field>
      <label>Phone</label>
      <value></value>
    </field>
    <field>
      <label>Years in Business</label>
      <value></value>
    </field>
    <field>
      <label>Background</label>
      <value>Background</value>
    </field>
    <field>
      <label>Place of Birth</label>
      <value>Earth</value>
    </field>
    <field>
      <label>Date of Birth</label>
      <value></value>
    </field>
    <field>
      <label>Some Label</label>
      <value>Some Label</value>
    </field>
    <field>
      <label>Industry</label>
      <value> Technology  Other</value>
    </field>
    <field>
      <label>Pets</label>
      <value>Dog</value>
    </field>
    <field>
      <label>Your View</label>
      <value>Positive</value>
    </field>
    <field>
      <label>Misc</label>
      <value>Misc</value>
    </field>
    <field>
      <label>Comments</label>
      <value></value>
    </field>
    <field>
      <label>Agree to Terms?</label>
      <value>True</value>
    </field>
  </fields>
</response>


<?xml version="1.0"?>
<response>
  <properties id="60375c90-9dd7-400f-aafb-a8726df409a9" Form="Account Request" Date="Tuesday, March 16, 2010 6:21:07 PM" Page="http://mydomain/sitefinity/Home.aspx" IP="fe80::1c0f57:9ee3%10" Browser="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30729)" Referrer="http://mydomain/sitefinity/Home.aspx" Subject="Email from website: Account Request Form" Sender="[email protected]" Recipients="[email protected]" Confirmation="True" />
  <fields>
    <field>
      <label>Personal Details</label>
      <value>Personal Details</value>
    </field>
    <field>
      <label>Name</label>
      <value>erger</value>
    </field>
    <field>
      <label>Email</label>
      <value></value>
    </field>
    <field>
      <label>Website</label>
      <value></value>
    </field>
    <field>
      <label>Password</label>
      <value></value>
    </field>
    <field>
      <label>Phone</label>
      <value></value>
    </field>
    <field>
      <label>Years in Business</label>
      <value></value>
    </field>
    <field>
      <label>Background</label>
      <value>Background</value>
    </field>
    <field>
      <label>Place of Birth</label>
      <value>Earth</value>
    </field>
    <field>
      <label>Date of Birth</label>
      <value></value>
    </field>
    <field>
      <label>Some Label</label>
      <value>Some Label</value>
    </field>
    <field>
      <label>Industry</label>
      <value> Technology  Service</value>
    </field>
    <field>
      <label>Pets</label>
      <value>Dog</value>
    </field>
    <field>
      <label>Your View</label>
      <value>Positive</value>
    </field>
    <field>
      <label>Misc</label>
      <value>Misc</value>
    </field>
    <field>
      <label>Comments</label>
      <value></value>
    </field>
    <field>
      <label>Agree to Terms?</label>
      <value>True</value>
    </field>
  </fields>
</response>

<?xml version="1.0"?>
<response>
  <properties id="60375c90-9dd7-400f-aafb-a8726df409a9" Form="Account Request" Date="Tuesday, March 16, 2010 4:50:17 PM" Page="http://mydomain/sitefinity/Home.aspx" IP="fe80::1c0f:ee3%10" Browser="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; SLCC1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.5.21022; .NET CLR 3.5.30729; .NET CLR 3.0.30729)" Referrer="http://mydomain/sitefinity/Home.aspx" Subject="Email from website: Account Request Form" Sender="[email protected]" Recipients="[email protected]" Confirmation="True" />
  <fields>
    <field>
      <label>Personal Details</label>
      <value>Personal Details</value>
    </field>
    <field>
      <label>Name</label>
      <value>esfs</value>
    </field>
    <field>
      <label>Email</label>
      <value></value>
    </field>
    <field>
      <label>Website</label>
      <value></value>
    </field>
    <field>
      <label>Password</label>
      <value></value>
    </field>
    <field>
      <label>Phone</label>
      <value></value>
    </field>
    <field>
      <label>Years in Business</label>
      <value></value>
    </field>
    <field>
      <label>Background</label>
      <value>Background</value>
    </field>
    <field>
      <label>Place of Birth</label>
      <value>Earth</value>
    </field>
    <field>
      <label>Date of Birth</label>
      <value></value>
    </field>
    <field>
      <label>Some Label</label>
      <value>Some Label</value>
    </field>
    <field>
      <label>Industry</label>
      <value> Technology  Service</value>
    </field>
    <field>
      <label>Pets</label>
      <value>Dog</value>
    </field>
    <field>
      <label>Your View</label>
      <value>Positive</value>
    </field>
    <field>
      <label>Misc</label>
      <value>Misc</value>
    </field>
    <field>
      <label>Comments</label>
      <value></value>
    </field>
    <field>
      <label>Agree to Terms?</label>
      <value>True</value>
    </field>
  </fields>
</response>

Can anyone help with this?

+3  A: 

First, you need to load the string into an XDocument for easy querying:

var xdoc = XDocument.Parse(xmlResponse);

Then you can use the methods of the XDocument class (e.g. Elements("Blah") and Attributes("Blah")) to extract the values that you want.

I don't think you're going to find anybody who is simply going to write the whole thing for you: that's your job. If you can tell us specifically what you're having trouble with, then we can give you some more help.

Dean Harding
Right, my problem is building the CSV text. Is there any builtin CSV object like XDocument? Otherwise, I would have to escape characters and manually delimit the data which could get messy. Also, how do I approach performance in case there is 2,500 iterations to build a big CSV file (stream I think it is)?
TruMan1
Have a look at Linq to CSV. http://www.codeproject.com/KB/linq/LINQtoCSV.aspx
mhanney
A: 

Use XSLT to convert the XML to CSV. Here is a previous Stackoverflow answer
http://stackoverflow.com/questions/365312/xml-to-csv-using-xslt

And here is a link with C# code to implement the XSLT in .Net 2.0

http://stackoverflow.com/questions/952365/how-do-i-write-an-xslt-to-transform-xml-to-csv

ggonsalv