首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Xdoc读取Atom

使用Xdoc读取Atom
EN

Stack Overflow用户
提问于 2011-05-01 01:43:14
回答 1查看 1.2K关注 0票数 1
代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8" standalone="yes" ?> 
- <feed xml:base="http://localhost:32026/Northwind/Northwind.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Categories</title> 
  <id>http://localhost:32026/Northwind/Northwind.svc/Categories/</id> 
  <updated>2011-04-30T17:15:09Z</updated> 
  <link rel="self" title="Categories" href="Categories" /> 
- <entry>
  <id>http://localhost:32026/Northwind/Northwind.svc/Categories(1)</id> 
  <title type="text" /> 
  <updated>2011-04-30T17:15:09Z</updated> 
- <author>
  <name /> 
  </author>
  <link rel="edit" title="Category" href="Categories(1)" /> 
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/Products" type="application/atom+xml;type=feed" title="Products" href="Categories(1)/Products" /> 
  <category term="NorthwindModel.Category" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" /> 
- <content type="application/xml">
- <m:properties>
  <d:CategoryID m:type="Edm.Int32">1</d:CategoryID> 
  <d:CategoryName>Beverages</d:CategoryName> 
  <d:Description>Soft drinks, coffees, teas, beers, and ales</d:Description> 
  <d:Picture m:type="Edm.Binary">FRwvAAIAAAANAA4AFAA...</d:Picture> 
  </m:properties>
  </content>

我正在尝试使用XDocument来压缩提要。我只想得到CategoryId,CategoryName,描述和图片。

我有这个代码,但是它不能工作..

代码语言:javascript
复制
//Namesapces
                //xml:base="http://localhost:32026/Northwind/Northwind.svc/" 
                XNamespace nsBase = "http://localhost:32026/Northwind/Northwind.svc/";

                //xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" 
                XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";

                //xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" 
                XNamespace m = "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata";

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


                    var xdoc = XDocument.Load("xmlfile stream");

                    foreach (var entity in xdoc.Descendants(atom + "entry")) {
                        var properties = entity.Descendants(m + "properties");
                        var category = new CategoryModel() {
                            Id = Convert.ToInt32(properties.Elements(d + "CategoryID")),
                            Name = properties.Elements(d + "CategoryName").ToString(),
                            Description = properties.Elements(d + "Description").ToString(),
                        };
                        Items.Add(category);
                    }

我也试过这种方法,但还是不行。

代码语言:javascript
复制
var properties = entity.Elements(atom + "content").Elements(m + "properties");
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-01 12:40:45

我知道了..。我输入"Element“而不是”Element“是错误的..

下面是正确的代码..

代码语言:javascript
复制
 foreach (var entity in xdoc.Descendants(atom + "entry")) {
                        var properties = entity.Element(atom + "content").Element(m + "properties");
                        var category = new CategoryModel() {
                            Id = Convert.ToInt32(properties.Element(d + "CategoryID").Value),
                            Name = properties.Element(d + "CategoryName").Value,
                            Description = properties.Element(d + "Description").Value,
                        };
                        Items.Add(category);
                    }
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5843529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档