使用C#将XML的属性值解析为CSV

时间:2017-06-22 15:38:41

标签: c# xml xml-parsing linq-to-xml

我将以下xml作为主xml的一部分。能够将其提取为字符串,现在我想解析下面的xml并将属性名称PersonN,VerifiedHuman,CurrAddrBlockIndex的值获取为单独的csv文件 请让我知道如何克服这个问题?

我想解析的XML:

<InterConnectResponse>
  <SchemaVersion>2.0</SchemaVersion>
  <ConsumerSubjects>
    <ConsumerSubject subjectIdentifier="Primary">
      <DataSourceResponses>
      <RiskViewProducts>
          <RiskViewAttribResponse>
          <Attributes>
                <Attribute>
                  <Name>PersonN</Name>
                  <Value>3</Value>
                </Attribute>
                <Attribute>
                  <Name>VerifiedHuman</Name>
                  <Value>2</Value>
                </Attribute>
                <Attribute>
                  <Name>CurrAddrBlockIndex</Name>
                  <Value>0.61</Value>
                </Attribute>
         </Attributes>
         </RiskViewAttribResponse>
     </RiskViewProducts>
     </DataSourceResponses>
    </ConsumerSubject>
  </ConsumerSubjects>
</InterConnectResponse>   

预期的输出文件:

3, 2, 0.61    

我试过这个但没有成功

  StringBuilder output = new StringBuilder();   
  using (XmlReader reader = XmlReader.Create(new StreamReader(value)))
            {

                reader.ReadToFollowing("PersonN");
                string LNREF72 = reader.Value;
                output.AppendLine(LNREF72);
                reader.ReadToFollowing("VerifiedHuman");
                string VerifiedHuman = reader.Value;
                output.AppendLine(", " + VerifiedHuman);
                reader.ReadToFollowing("CurrAddrBlockIndex");
                string CurrAddrBlockIndex = reader.Value;
                output.AppendLine(", " + CurrAddrBlockIndex);

            }     

1 个答案:

答案 0 :(得分:0)

不是我会这样做但是如果你打算使用XmlReader,这将是你想要的方式。

请查看XmlDocument或其他十几种方法之一来解析优于XmlReader的XML。

  // regular forward node
  var x0 = d.source.x + d.source.dx,
      x1 = d.target.x,
      xi = d3.interpolateNumber(x0, x1),
      x2 = xi(curvature),
      x3 = xi(1 - curvature),
      y0 = d.source.y + d.sy,
      y1 = d.target.y + d.ty;
  return "M" + x0 + "," + y0
       + "C" + x2 + "," + y0
       + " " + x3 + "," + y1
       + " " + x1 + "," + y1
       // move down for the wanted width
       + "l" + 0  + "," + d.dy
       // draw another path below mirroring the top
       + "C" + x3 + "," + (y1 + d.dy)
       + " " + x2 + "," + (y0 + d.dy)
       + " " + x0 + "," + (y0 + d.dy);