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

StackOverflow https://stackoverflow.com/questions/21224905

  •  30-09-2022
  •  | 
  •  

Вопрос

The Xml for the file

   <?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.

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'. How to get the values of every element in 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. 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.

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? https://www.nuget.org/packages?q=atom

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top