【问题标题】:Linq namespace issueLinq 命名空间问题
【发布时间】:2009-11-02 20:24:44
【问题描述】:

我似乎可以从以下 xml 中获取任何记录。

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

如果我删除上述内容,它会起作用

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

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

    你的代码对我来说很好用:

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

    (我删除了查询表达式,因为它什么也没做,但在那之前它也有效。)

    这只是将test.xml 设置为您在问题中显示的 XML。

    如果你用失败的代码打印出feed,你会看到什么?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-28
      • 1970-01-01
      • 2011-02-05
      • 2013-03-26
      • 2016-05-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多