2011-04-30 3 views
1
<?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> 

lecture Atom en utilisant xdoc

Je suis en train de pares l'alimentation en utilisant XDocument .. Je veux juste obtenir le CategoryId, CategoryName, Description et image.

Je possède ce code, mais il ne fonctionne pas ..

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

J'ai essayé de cette façon aussi, mais ne fonctionne toujours pas

var properties = entity.Elements(atom + "content").Elements(m + "properties"); 

Répondre

1

Je l'ai .. Je typé « Elements » au lieu de "Element" mal ..

Voici le code correct ..

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

besoin d'attendre 2 jours pour accepter ma propre réponse ... :( –