开发者

Linq namespace issue

开发者 https://www.devze.com 2022-12-10 05:32 出处:网络
I can seem to get any records from开发者_运维问答 the following xml. xmlns=\"http://www.w3.org/2005/Atom\"

I can seem to get any records from开发者_运维问答 the following xml.

xmlns="http://www.w3.org/2005/Atom"

If i remove the above it works

  <?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<entry><id><![CDATA[text]]></id><
author><name><![CDATA[film24]]></name></author><title><![CDATA[text]]></title>
<updated>2009-10-30T15:55:13+00:00</updated><published>2009-10-30T00:00:00+00:00</published>
<media:thumbnail type="image/jpeg" title="thumbnail" url=""/>
<media:content type="video/flv" title="video" url="" expression="high"/>
<media:content type="video/flv" title="video" url="" expression="low"/>
</entry>
</feed> 





//My linq 
        XNamespace nsAtom = "http://www.w3.org/2005/Atom";

        string url = HttpContext.Current.Server.MapPath(ConfigHelper.GetValue("FeedUri"));
        var feed = XElement.Load(url); 
        var query = from c in feed.Descendants(nsAtom + "entry")
        select c;
        return query.Count(); // always returns o


Your code works fine for me:

using System;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Linq;

class Test    
{    
    static void Main()
    {
        XNamespace nsAtom = "http://www.w3.org/2005/Atom";
        var feed = XElement.Load("test.xml");
        Console.WriteLine(feed.Descendants(nsAtom + "entry").Count());
    }
}

(I've removed the query expression because it wasn't doing anything, but it worked before then too.)

That's just with test.xml set to the XML you've shown in the question.

If you print out feed with your failing code, what do you see?

0

精彩评论

暂无评论...
验证码 换一张
取 消