开发者

Reading Atom by using Xdoc

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

I'm trying to pares the feed by using XDocument.. I just want to get the CategoryId, CategoryName, Description and Picture.

I have this code but it doesn't work..

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

I tried this way as well but still not working

var pro开发者_JAVA技巧perties = entity.Elements(atom + "content").Elements(m + "properties");


I got it.. I typed "Elements" instead of "Element" wrongly..

Here is the correct code ..

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜