简体   繁体   English

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

[英]Parsing Attribute values of XML into a CSV using C#

I have the below xml as a part of a main xml. 我将以下xml作为主要xml的一部分。 was able to extract this into a string, now I want to parse the below xml and get the values of the Attribute names PersonN , VerifiedHuman , CurrAddrBlockIndex into a seperate csv file Please let me know how to overcome this issue ? 能够将其提取到字符串中,现在我想解析以下xml,并将属性名称PersonN,VerifiedHuman和CurrAddrBlockIndex的值保存到单独的csv文件中,请让我知道如何解决此问题?

XML I am trying to parse : 我正在尝试解析的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>   

Expected Output file : 预期输出文件:

3, 2, 0.61    

I tried this but did not succeed 我尝试了这个但没有成功

  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);

            }     

Not the way I would do it but if you are going to use XmlReader this would be the way to do what you want. 不是我要这样做的方式,但是如果您要使用XmlReader,这将是您想要做的方式。

Please look into XmlDocument or one of a dozen other way to parse XML that is better than XmlReader. 请研究XmlDocument或其他比XmlReader更好的解析XML的方法之一。

StringBuilder output = new StringBuilder();
using (XmlReader reader = XmlReader.Create(new StreamReader(value)))
{
    bool isValue = false;
    while (reader.Read())
    {
        if (reader.NodeType == XmlNodeType.Element && reader.Name == "Value")
        {
            isValue = true;
        }

        if (reader.NodeType == XmlNodeType.Text && isValue)
        {
            output.AppendLine((output.Length == 0 ? "" : ", ") + reader.Value);
            isValue = false;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM