C#:使用XDocument将XML解析为csv文件

时间:2017-06-23 21:34:28

标签: c# xml linq csv

我有几种格式的xmls:

<InterConnectResponse>
  <SchemaVersion>2.0</SchemaVersion>
  <ConsumerSubjects>
    <ConsumerSubject subjectIdentifier="Primary">
      <DataSourceResponses>
      <RiskViewProducts>
          <RiskViewAttribResponse>
          <Attributes>
                <Attribute>
                  <Name>CurrAddrTaxValue</Name>
                  <Value>3</Value>
                </Attribute>
                <Attribute>
                  <Name>CurrAddrTaxMarketValue</Name>
                  <Value>2</Value>
                </Attribute>
                <Attribute>
                  <Name>CurrAddrBlockIndex</Name>
                  <Value>0.61</Value>
                </Attribute>
           ------ Many More Attributes ---------
         </Attributes>
         </RiskViewAttribResponse>
     </RiskViewProducts>
     </DataSourceResponses>
    </ConsumerSubject>
  </ConsumerSubjects>
</InterConnectResponse> 

我想只解析上面xml中的特定属性。所以我用下面的logi。但是如何将值的结果(仅值而不是名称)保存为csv文件?

 var document = XDocument.Parse(str3); // or `= XDocument.Parse(xml);`
 var attributesToRead = new[] { "CurrAddrTaxValue", "CurrAddrTaxMarketValue", "PrevAddrTaxValue", "PrevAddrAVMValue", "AddrChangeCount60", "DerogSeverityIndex", "LienFiledCount03", "LienSmallClaimsFiledTotal", "EvictionCount12", "NonDerogCount", "NonDerogCount12", "InquiryPersonalFinanceRecent", "HighRiskCreditActivity", "SubPrimeOfferRequestCount", "SubPrimeOfferRequestCount60" };
 var productsElements = document.XPathSelectElements("InterConnectResponse/ConsumerSubjects/ConsumerSubject/DataSourceResponses/RiskViewProducts");
 var products = productsElements.Select(product => new
     {
         Attributes = product.XPathSelectElements("RiskViewAttribResponse/Result/Attributes/Attribute").Select(attribute => new
         {
              Name = attribute.XPathSelectElement("Name").Value,
              Value = attribute.XPathSelectElement("Value").Value
          }).Where(attribute => attributesToRead.Contains(attribute.Name))
     });

但是如何将结果写入csv,当我解析下一个xml时它会附加? Aso我只想将值写入csv而不是属性的名称..

所以我的预期输出是:

3, 2, 0.61,  ............

2 个答案:

答案 0 :(得分:0)

您的products变量是匿名类型的IEnumerable,其中包含名为Attributes的属性。 Attributes属性和IEnumerable匿名类型再次包含2个属性:NameValue

您希望将所有Value属性的内容写入(准确地说是附加到)文件中,然后执行以下操作:

var values = products.SelectMany(x => x.Attributes).Select(x => x.Value);
File.AppendAllText("someFileName.csv", string.Join(",", values));

当然,我假设您的代码在此之前有效,并且您只是难以附加到csv文件。

答案 1 :(得分:0)

您是否考虑过使用库。使用Cinchoo ETL - 一个开源ETL库,您可以用几行代码生成预期的输出。它是基于流的,可以解析任何大小的xml。

using (var parser = new ChoXmlReader("sample.xml").WithXPath("Attributes/Attribute")
    .WithField("Name", xPath: "Name")
    .WithField("Value", xPath: "value")
    )
{
    using (var writer = new ChoCSVWriter("sample.csv"))
        writer.Write(parser.Select(kvp => kvp.Value).ToExpandoObject());
}

输出结果为:

3,2,0.61

披露:我是这个图书馆的作者。