开发者

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

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="admin@admin.com" recipients="test@test.com, , " />
  <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>test@test.com</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="admin@admin.com" Recipients="test@test.com" Confirmation="True" />
  <fields>
    <field>
      <label>Personal Details</label>
      <value>Personal Details</value>
    </field>
    <field>
      <label>Name</label>
      <value>erger</value>
    <开发者_如何学Python/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="admin@admin.com" Recipients="test@test.com" 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?


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.


Use XSLT to convert the XML to CSV. Here is a previous Stackoverflow answer
XML to CSV Using XSLT

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

How do I write an XSLT to transform XML to CSV?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜