有人可以告诉我为什么这个程序不列举任何项目?它与RDF命名空间有关吗?
- using System;
- using System.Xml.Linq;
- using System.Xml.XPath;
- class Program
- {
- static void Main(string[] args)
- {
- var doc = XDocument.Load("http://seattle.craigslist.org/sof/index.RSS");
- foreach (var item in doc.XPathSelectElements("//item"))
- {
- Console.WriteLine(item.Element("link").Value);
- }
- Console.Read();
- }
- }
解决方法
是的,它绝对是命名空间 – 尽管它是RSS命名空间,而不是RDF.你试图找到没有命名空间的项目.
在.NET中使用XPath中的命名空间有点棘手,但在这种情况下,我只是使用LINQ to XML Descendants方法:
- using System;
- using System.Linq;
- using System.Xml.Linq;
- class Test
- {
- static void Main()
- {
- var doc = XDocument.Load("http://seattle.craigslist.org/sof/index.RSS");
- XNamespace RSS = "http://purl.org/RSS/1.0/";
- foreach (var item in doc.Descendants(RSS + "item"))
- {
- Console.WriteLine(item.Element(RSS + "link").Value);
- }
- Console.Read();
- }
- }