0

HI i am trying to read xml file but i am not able to get the elemets here is my sample code

<?xml version="1.0" encoding="utf-8"?>
    <feed xmlns="http://www.w3.org/2005/Atom">
        <title>Read xml file</title>
        <link href=""></link>
        <updated>2014-01-06T10:32:04.230060</updated>
        <author><name>rss</name></author>

        <entry>
            <title>abc: Watching &#34;Mary Tyler Moore.&#34; Every character is drinking Scotch. #The70s </title>
            <link href="http://RobertOSimonson/status/417858586618781697"></link>
            <id>/RobertOSimonson/status/417858586618781697</id>
            <updated>2013-12-30T19:23:00Z</updated>
            <published>2013-12-30T19:23:00Z</published>
            <summary>Watching &#34;Mary Tyler Moore.&#34; Every character is drinking Scotch. #The70s </summary>
        </entry>

        <entry>
            <title>abc: Galliano makes very appropriate cameo in the &#39;70s-set &#34;American Hustle.&#34; #AmericanHustle </title>
            <link href="http://RobertOSimonson/status/417511925258272768"></link>
            <id>/RobertOSimonson/status/417511925258272768</id>
            <updated>2013-12-29T20:26:00Z</updated>
            <published>2013-12-29T20:26:00Z</published>
            <summary>Galliano makes very appropriate cameo in the &#39;70s-set &#34;American Hustle.&#34; #AmericanHustle </summary>
        </entry>
</feed>

and in my C# code i am trying like this

var posts = (from p in rssFeed.Root.Elements("entry")
             select new
             {
                 Title = p.Element("title").Value,
                 Link = p.Element("link").Value,
             }).ToList();

foreach (var post in posts)
{
    Response.Write(post.Title + "<br>");
}

Please Suggest what i am doing wrong. i want to read what is inside Thanks

8
  • 1
    Your XML sample isn't valid, you're missing the closing </feed> Commented Jan 6, 2014 at 12:02
  • Edited it is there now.. i didnt add it in the question Commented Jan 6, 2014 at 12:04
  • is there any error? exception? what is the problem? Commented Jan 6, 2014 at 12:16
  • NO the page just comes blank.. nothing appears and no error Commented Jan 6, 2014 at 12:16
  • is there anything in posts when you debug? Commented Jan 6, 2014 at 12:21

1 Answer 1

1

You need to include the xml namespace in your queries:

XNamespace ns = "http://www.w3.org/2005/Atom";
var posts = (from p in rssFeed.Root.Elements(ns + "entry")
             select new
             {
                 Title = p.Element(ns + "title").Value,
                 Link = p.Element(ns + "link").Value,
             }).ToList();

foreach (var post in posts)
{
    Console.WriteLine(post.Title);
}

Check out the linq to xml samples for C# here: http://msdn.microsoft.com/en-us/library/bb397976.aspx

Sign up to request clarification or add additional context in comments.

1 Comment

Perfect i have a Response Object now :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.