How can I convert XML files to one CSV file in C#?

Posted by TruMan1 on Stack Overflow See other posts from Stack Overflow or by TruMan1
Published on 2010-05-04T23:46:32Z Indexed on 2010/05/05 2:28 UTC
Read the original article Hit count: 423

Filed under:
|
|
|
|

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?

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET