2014-01-20 248 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++); 
    } 
});