Question

My xml format is like

  <?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 written the code with some help so as to parse the Xml and get the data as

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

My desired output is -

  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

But the output I am getting from the above piece of code is :

 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]

I am getting the output as specified above and I am not quite sure why am I getting the above output. I desire to get the result for each 'Url' as is specified in the desired output. been fighting for a while to get the desired result.

Any help is appreciated. Novice programmer, new to C# not sure how to get the output. Is this because of the wrong XML format?

Was it helpful?

Solution

Your code is trying to call ToString() on a Linq expression which is why you're seeing that System.Linq.Enumerable+ gobbledygook.

You need to enumerate the Urls:

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

or a more succinct way to write this:

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++);
    }
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top