2013-07-21 7 views
2

Je développe un projet de sitemap et j'ai un problème;XMLDocument Accès au dernier enfant

c'est mon fichier xml

<?xml version="1.0" encoding="UTF-8"?> 
<urlset> 
<url ID="1"> 
<loc>http://www.serkancamur.com/Site/Index/sayfa/Hakkimda</loc> 
<changefreq>Daily</changefreq> 
<priority>0,9</priority> 
</url> 
</urlset> 

c'est mon code C#:

int ID = Convert.ToInt32(doc.SelectSingleNode("urlset").LastChild.Attributes["ID"].Value); 

cela fonctionne regarder, mais à urlset attributs élément:

<?xml version="1.0" encoding="UTF-8"?> 
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" schemaLocation="http://www.serkancamur.com/sitemap.xsd"> 
<url ID="1"> 
<loc>http://www.serkancamur.com/Site/Index/sayfa/Hakkimda</loc> 
<changefreq>Daily</changefreq> 
<priority>0,9</priority> 
</url> 
</urlset> 

i seulement ajouté attributs élément urlset, alors pourquoi cela ne fonctionne pas?

Répondre

1

Vous devez utiliser XmlNamespaceManager

Essayez cette

int id = 0; 
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); 
    nsmgr.AddNamespace("x", "http://www.sitemaps.org/schemas/sitemap/0.9"); 
    var urlset = doc.SelectSingleNode("//x:urlset", nsmgr); 
    id = Convert.ToInt32(urlset.LastChild.Attributes["ID"].Value); 

Hope this helps