如何解析XML以获取c#中的数据?

时间:2014-01-18 08:12:52

标签: c# xml csv

我有一个xml文件,我想将信息写入csv文件。

我正在尝试解析xml以获取字段和数据并写入csv文件。到目前为止,我已经能够解析数据以获取与之相关的信息 节点'entry'下的'title'和'id',但我试图在node'','id'信息下解析数据。

 <?xml version="1.0" encoding="utf-8" standalone="yes"?>
   <feed xml:base="http://google.com/en-US/syndicate/"  xmlns:d="http://schemas.google.com/ado/2007/08/dataservices" xmlns:m="http://schemas.giooglt.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
     <title type="text">Partners</title>
     <id>http://googlre.com/en-US/syndicate/Partners</id>
     <updated>2014-01-16T21:33:20Z</updated>
    < link rel="self" title="Partners" href="Partners" />
      <entry>
    <id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
   <title type="text">M55p; Co</title>
   <summary type="text">cccc is a Certified Partner, reseller, and implementer of 
   Key industries we work with include:
    •   Financial services
     •   Professional services
     •   Media / publishing


    By focusing on mid-market to enterprise clients, 
 </summary>
   <published>2009-07-21T14:23:50-07:00</published>
   <updated>2013-11-22T15:00:46-08:00</updated>
    <author>
    <name>google chrome</name>
    <uri>http://google.com/</uri>
    <email>retee@gmail.com</email>
   </author>
  <link rel="edit" title="Partner" href="Partners('4255')" />
   <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
   <m:inline>
     <feed>
   <title type="text">Links</title>
  <id>http://google.com/('429')/Links</id>
  <updated>2014-01-16T21:33:20Z</updated>
  <link rel="self" title="Links" href="Partners('4ff')/Links" />
  <entry>
    <id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
    <title type="text">
    </title>
    <updated>2014-01-16T21:33:20Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Link" href="Links('partnerpage')" />
    <category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Type>pgooglrpartnerpage</d:Type>
        <d:Description>google Partner Page</d:Description>
        <d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&amp;wt.mc_id=66ttet</d:Url>
      </m:properties>
    </content>
  </entry>
  </m:inline>
  </entry>
  <entry>
    <id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555')</id>
   <title type="text">M55p; Co</title>
   <summary type="text">cccc is a Certified Partner, reseller, and implementer of 
   Key industries we work with include:
    •   Financial services
     •   Professional services
     •   Media / publishing


    By focusing on mid-market to enterprise clients, 
 </summary>
   <published>2009-07-21T14:23:50-07:00</published>
   <updated>2013-11-22T15:00:46-08:00</updated>
    <author>
    <name>google chrome</name>
    <uri>http://google.com/</uri>
    <email>retee@gmail.com</email>
   </author>
  <link rel="edit" title="Partner" href="Partners('4255')" />
   <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Links" type="application/atom+xml;type=feed" title="Links" href="Partners('4559')/Links">
   <m:inline>
     <feed>
   <title type="text">Links</title>
  <id>http://google.com/('429')/Links</id>
  <updated>2014-01-16T21:33:20Z</updated>
  <link rel="self" title="Links" href="Partners('4ff')/Links" />
  <entry>
    <id>http://ryryr.com/en-US/syndicate/Links('ufufr')</id>
    <title type="text">
    </title>
    <updated>2014-01-16T21:33:20Z</updated>
    <author>
      <name />
    </author>
    <link rel="edit" title="Link" href="Links('partnerpage')" />
    <category term="google.Commerce.ferrr.Syndicate.V2010_05.Link" scheme="http://schemas.frrr.com/ado/2007/08/dataservices/scheme" />
    <content type="application/xml">
      <m:properties>
        <d:Type>pgooglrpartnerpage</d:Type>
        <d:Description>google Partner Page</d:Description>
        <d:Url>http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&amp;wt.mc_id=66ttet</d:Url>
      </m:properties>
    </content>
  </entry>
  </m:inline>
  </entry>
  </feed>

条目会有所不同,但正如人们所见,我想提取信息并将其写入CSV文件中 -

标题,ID,PinpointId //德:条目/解:标题,//德:条目/解:标题,//德:条目/解:链路/米:直列/解:进料/解:ID

每个条目。

用于获取信息的代码是:

     // Alternate Method for getting the Fields from the XML file
         XmlDocument xmlDocument = new XmlDocument();
         xmlDocument.Load("C:/Users/Administrator/Downloads/direct.xml");
        XmlNamespaceManager xmlnm = new XmlNamespaceManager(xmlDocument.NameTable);
        xmlnm.AddNamespace("de","http://www.w3.org/2005/Atom");
        xmlnm.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
        xmlnm.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices");

        ParseXML(xmlDocument, xmlnm);

        Debug.WriteLine("\n---XML parsed---");

    }

    public static void ParseXML(XmlDocument xmlFile, XmlNamespaceManager xmlnm)
    {


        String path = "C:/Users/Administrator/Downloads/data.csv";
        var w = new StreamWriter(path); 



        XmlNodeList nodes = xmlFile.SelectNodes("//de:entry", xmlnm);


        foreach (XmlNode node in nodes)
        {
            string titl = node["title"].InnerText;
            string ide = node["id"].InnerText;
            //string cty = node["link/m:inline/de:feed/de:id"].InnerText;
            Debug.WriteLine("Data :" + titl+"ID :"+ide);


        }

            }

     The output of the program is -

       Data :MIG & CoID :http://pinpoint.microsoft.com/en-US/syndicate/Partners('4295719419')

我正在尝试解析数据。           string cty = node [“link / m:inline / de:feed / de:id”]。InnerText;

但是产生了错误。           “对象引用未设置为对象的实例。”

xml有多个条目,格式相似。 C#的新手如何获取信息并将其写入CSV文件。请帮助我。

1 个答案:

答案 0 :(得分:0)

首先,你的xml不是很好。它需要纠正。其次,我不确定您尝试使用查询"link/m:inline/de:feed/de:id"

返回的节点到底是什么

话虽如此,你的node["link/m:inline/de:feed/de:id"]无效,所以它没有被设置为你期望的XmlNode对象。那是因为XmlNode项需要节点的名称,而不是xpath查询。如果要传递xpath查询,请使用SelectSingleNode。像这样:

foreach (XmlNode node in nodes)
{
    string titl = node["title"].InnerText;
    string ide = node["id"].InnerText;
    string cty;
    var ctyNode = node.SelectSingleNode("link/m:inline/de:feed/de:id");
    if (ctyNode != null)
    {
        cty = ctyNode.InnerText
    }
    Debug.WriteLine("Data :" + titl+"ID :"+ide);
}