解析复杂 XML,选择命名元素时不存在后代元素

Parsing complex XML, no descendent elements exists when selecting a named element

我在解析复杂的 XML 文件时遇到问题。我无法通过名字获得后代。我试图简单地获取 feed->entry->content->m:properties->d:title 元素,但我什至无法按名称获取第一个后代。请帮忙。

 string XML = @"<feed xmlns:base=""https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch"" 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"">sushi</title><subtitle type=""text"">Bing Related Search</subtitle><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'</id><rights type=""text"" /><updated>2015-11-23T22:33:13Z</updated><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=0&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">c6b512cc-e93a-48f4-9691-e61dc0c184df</d:ID><d:Title m:type=""Edm.String"">Sushi Near My Current Location</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Sushi+Near+My+Current+Location</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=1&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">49fb106e-d74c-440b-a4f7-8942f357fcf7</d:ID><d:Title m:type=""Edm.String"">Types of Sushi</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Types+of+Sushi</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=2&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">086097d2-243a-4376-9580-eee5ac721ec7</d:ID><d:Title m:type=""Edm.String"">Types of Sushi Rolls</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Types+of+Sushi+Rolls</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=3&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">0fa4548f-fda9-4d0a-88fc-e8952846e1d6</d:ID><d:Title m:type=""Edm.String"">Sushi Guide</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Sushi+Guide</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=4&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">a9a6bc5a-363d-48bf-87c1-f4dd71f9a18c</d:ID><d:Title m:type=""Edm.String"">Sushi Recipes for Beginners</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Sushi+Recipes+for+Beginners</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=5&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">d00b0830-8148-43cf-b3db-b7f9623e3428</d:ID><d:Title m:type=""Edm.String"">Sushi Places Near Me</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Sushi+Places+Near+Me</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=6&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">28a56f1f-a03a-4337-8576-3681fd38ec9b</d:ID><d:Title m:type=""Edm.String"">How to Make Sushi</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=How+to+Make+Sushi</d:BingUrl></m:properties></content></entry><entry><id>https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/RelatedSearch?Query='sushi'&amp;$skip=7&amp;$top=1</id><title type=""text"">RelatedSearchResult</title><updated>2015-11-23T22:33:13Z</updated><content type=""application/xml""><m:properties><d:ID m:type=""Edm.Guid"">e961500c-1068-4740-950a-0fdf48fe005f</d:ID><d:Title m:type=""Edm.String"">Sushi Rolls Pictures and Names</d:Title><d:BingUrl m:type=""Edm.String"">http://www.bing.com/search?q=Sushi+Rolls+Pictures+and+Names</d:BingUrl></m:properties></content></entry></feed>";

        XElement root = XElement.Parse(XML);

        //Has descendants, including entry items.
        var capabilities1 = root.Descendants();

        //No Results for any of these. 
        var capabilities2 = root.Descendants("entry");
        var capabilities3 = root.Elements("entry");
        var capabilities4 = root.Descendants().Where(c => c.Name == "entry");

你什么也得不到,因为他们使用默认的 xml 命名空间,在你的情况下,它是 xmlns="http://www.w3.org/2005/Atom".

如果你想得到它们,你必须执行以下代码:

XNamespace ns = "http://www.w3.org/2005/Atom";
var entryFullName = ns + "entry";
var capabilities2 = root.Descendants(entryFullName);
var capabilities3 = root.Elements(entryFullName);
var capabilities4 = root.Descendants().Where(c => c.Name == entryFullName);

如果要钻取到 d:Title 元素,则必须像我为默认命名空间所做的那样为 d 命名空间声明一个 XNamespace。所有别名都在根元素中为您声明。如果您看到一个元素或属性带有字母或字符串,后跟 : 字符,那么请检查名称空间的定义位置(通常在根元素处)并在检查代码中的文档时使用它。

要检索 d:Title,您必须执行以下操作:

XNamespace ns = "http://www.w3.org/2005/Atom";
XNamespace d = "http://schemas.microsoft.com/ado/2007/08/dataservices";
var entryFullName = ns + "entry";
var capabilities3 = root.Elements(entryFullName).Descendants(d + "Title");