2014-01-20 640 views
1

我的XML格式是這樣如何從xml中獲取多個值?

<?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" sch="" eme="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> 
     <entry> 
     <id>http://googlet.com/en-US/syndicate/Links('tpartnerrfipage')</id> 
     <title type="text"> 
     </title> 
     <updated>2014-01-19T04:01:49Z</updated> 
     <author> 
      <name /> 
     </author> 
     <link rel="edit" title="Link" href="Links('pinpointpartnerrfipage')" /> 
     <category term="google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.google.com/ado/2007/08/dataservices/scheme" /> 
     <content type="application/xml"> 
      <m:properties> 
      <d:Type>tpartnerrfipage</d:Type> 
      <d:Description>RFI Page</d:Description> 
      <d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&amp;wt.mc_id=54545</d:Url> 
      </m:properties> 
     </content> 
     </entry> 
    </feed> 
    </m:inline> 
    </link> 
</entry> 
    <entry> 
     <id>http://pinpoint.microsoft.com/en-US/syndicate/Partners('45')</id> 
     <title type="text">vfere</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> 
     <entry> 
     <id>http://googlet.com/en-US/syndicate/Links('tpartnerrfipage')</id> 
     <title type="text"> 
     </title> 
     <updated>2014-01-19T04:01:49Z</updated> 
     <author> 
      <name /> 
     </author> 
     <link rel="edit" title="Link" href="Links('pinpointpartnerrfipage')" /> 
     <category term="google.Commerce.Marketplace.Syndicate.V2010_05.Link" scheme="http://schemas.google.com/ado/2007/08/dataservices/scheme" /> 
     <content type="application/xml"> 
      <m:properties> 
      <d:Type>tpartnerrfipage</d:Type> 
      <d:Description>RFI Page</d:Description> 
      <d:Url>http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&amp;wt.m</d:Url> 
      </m:properties> 
     </content> 
     </entry> 
     </feed> 
    </m:inline> 
    </link> 
    </entry> 
    </feed> 

我寫的代碼有一定的幫助,以解析XML和獲取數據的

var reader = new StreamReader(@"C:/Users/Administrator/Downloads/direct.xml") 

var xmlDoc = XDocument.Load(reader); 

XNamespace atom = "http://www.w3.org/2005/Atom"; 
XNamespace metadata = "http://schemas.giooglt.com/ado/2007/08/dataservices/metadata"; 
XNamespace dataservices = "http://schemas.google.com/ado/2007/08/dataservices"; 

var result = xmlDoc.Root.Elements(atom + "entry") 
    .Select(e => new { 
     Title = e.Element(atom + "title").Value, 
     Id = e.Element(atom + "id").Value, 
     Urls = e.Elements(atom + "link") 
      .Where(l => l.Element(metadata + "inline") != null) 
      .SelectMany(l => l.Element(metadata + "inline") 
       .Element(atom + "feed") 
       .Elements(atom + "entry") 
       .Select(e1 => e1.Element(atom + "content") 
        .Element(metadata + "properties") 
        .Element(dataservices + "Url").Value)) 
     }); 

foreach (var item in result) 
{ 
    Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id,item.Urls); 
    Debug.WriteLine(item.Urls.GetType()); 
    foreach(var i in item.Urls){ 
     Debug.WriteLine("i :"+i); 
    } 
} 

我期望的輸出 -

M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://googlgt.com/en-US/PartnerDetails.aspx? PartnerId=42555&amp;wt.mc_id=66ttet 
    M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),http://pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&amp;wt.mc_id=54545 
    vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&amp;wt.mc_id=66ttet 
    vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http: //pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&amp;wt.m 

但是我從上面這段代碼得到的輸出是:

M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]  
M55p; Co,http://pinpoint.microsoft.com/en-US/syndicate/Partners('4555'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String] 
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String] 
vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String] 

我得到的輸出如上所述,我不太清楚爲什麼我會得到上面的輸出。我希望按照所需輸出中指定的方式獲取每個「Url」的結果。一直在爭取一段時間以獲得理想的結果。

任何幫助表示讚賞。新手程序員,新來的C#不知道如何獲得輸出。這是因爲XML格式錯誤嗎?

回答

0

你的代碼試圖在Linq表達式上調用ToString(),這就是你看到System.Linq.Enumerable + gobbledygook的原因。

您需要枚舉的URL:

foreach (var item in result) 
{ 
    var i = 0; 
    foreach (var url in item.Urls) 
    { 
     Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id, url); 
     Debug.WriteLine(url.GetType()); 
     Debug.WriteLine("i : " + i++); 
    } 
} 

或寫這個更簡潔的方式:

result.ToList().ForEach(item => 
{ 
    var i = 0; 
    foreach (var url in item.Urls) 
    { 
     Debug.WriteLine("{0}, {1}, {2}", item.Title, item.Id, url); 
     Debug.WriteLine(url.GetType()); 
     Debug.WriteLine("i : " + i++); 
    } 
});