简体   繁体   English

如何获取“无穷大”中的每个值 <String> 在C#中显示多值xml文件吗?

[英]How to get the each values in 'Ienumerable <String> Urls' in c# for a multiple value xml file?

The Xml for the file 该文件的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>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" 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>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>
    <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>

I have a piece of code where I want to simply console output the values of each Urls. 我有一段代码,我想在其中简单地控制台输出每个Urls的值。

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").Elements.Select(url => url.Value)))
        });

  foreach (var item in result)
     {
    Debug.WriteLine("{0}, {1}", item.Title, item.Id);
    var urls = System.String.Join(",",item.Urls);
    Debug.WriteLine(urls);

  }

Here Urls are of type 'Ienumerable Urls'. 这里的Urls是“无数的Urls”类型。 How to get the values of every element in Urls. 如何获取Urls中每个元素的值。

desired output - 期望的输出-

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

Any help is appreciated. 任何帮助表示赞赏。

Actual Output 实际产量

 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,System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
 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,System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String] 
 vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http://googlgt.com/en-US/PartnerDetails.aspx?PartnerId=42555&amp;wt.mc_id=66ttet, System.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]
 vfere,http://pinpoint.microsoft.com/en-US/syndicate/Partners('45'),http: //pinpoint.microsoft.com/en-US/RFI.aspx?partnerId=4295719419&amp;wt.mSystem.Linq.Enumerable+<SelectManyIterator>d__14`2[System.Xml.Linq.XElement,System.String]

Ah, I see. 知道了 You want a comma-separated list of the URLs. 您需要用逗号分隔的URL列表。 As you pointed out, your Urls property is an IEnumerable of strings, so you need to actually iterate over those strings to build the final string you're looking for. 如您所指出的,您的Urls属性是一个IEnumerable字符串,因此您实际上需要遍历这些字符串以构建您要查找的最终字符串。

Try this: 尝试这个:

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 r in result)
{
    Debug.WriteLine("{0},{1},{2}", r.Title, r.Id, String.Join(",", r.Urls));
}

Just curious: since you're apparently parsing an ATOM feed, have you looked into packages that will do this for you? 只是好奇:既然您显然是在解析ATOM提要,那么您是否研究过可以为您做到这一点的软件包? https://www.nuget.org/packages?q=atom https://www.nuget.org/packages?q=atom

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

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