<?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来削减Feed ..我只想获取CategoryId,CategoryName,Description和Picture。
我有这段代码,但它不起作用..
//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);
}
我也尝试过这种方式,但仍无法正常工作
var properties = entity.Elements(atom + "content").Elements(m + "properties");
答案 0 :(得分:1)
我明白了..我错误地输入了“元素”而不是“元素”..
这是正确的代码..
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);
}