如何在C#中将XML文件转换为一个CSV文件?

时间:2010-05-04 23:46:32

标签: c# asp.net xml .net-3.5 csv

我有一组XML内容的字符串。我希望通过我的集合进行迭代并构建一个CSV文件以流式传输给用户进行下载(有时它可能在集合中有数百个)。这是我的循环:

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

    //BUILD CSV HERE
}

这是我的XML内容对于每次迭代(xmlResponse)的样子。我想把它放在一个包含“属性”属性的平面文件中:

<?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>
    </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>

任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:3)

首先,您需要将字符串加载到XDocument以便于查询:

var xdoc = XDocument.Parse(xmlResponse);

然后,您可以使用XDocument类的方法(例如Elements("Blah")Attributes("Blah"))来提取您想要的值。

我认为你不会找到任何只是为你写下整件事的人​​:那是你的工作。如果你能具体告诉我们你遇到什么问题,那么我们可以给你一些帮助。

答案 1 :(得分:0)

使用XSLT将XML转换为CSV。 这是以前的Stackoverflow答案
XML to CSV Using XSLT

这是一个用C#代码链接来实现.Net 2.0中的XSLT

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