2014-01-18 182 views
0

我有一個xml文件,我想將信息寫入csv文件。如何解析XML以獲取c#中的數據?

我想解析xml以獲取字段和數據並寫入到csv文件。到目前爲止,我已經能夠解析數據以獲得與節點'entry'下的'title'和'id'有關的信息 ,但我試圖解析節點'','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>[email protected]</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>[email protected]</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 //德:進入/ DE: title,// de:entry/de:title,// de:entry/de:link/m:inline/de:feed/de:id

for each and entry。

的一段代碼被用於獲取的信息是:

 // 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文件。請幫助我。

回答

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); 
} 
+0

但是,如果有喜歡的多個條目 - blblbl blblbl那麼我是如何將得到的數據,因爲上述修改的步驟給我回了第一個「入口」 – user2825395

+0

裏面只是數據現在,因爲你的XML格式不正確,很難分辨你的需求是什麼。首先要着手解決你的xml問題。 –

+0

我認爲新的編輯會給你一個更好的主意。 – user2825395

相關問題