[英]XDocument, XElement : Sequence contains no matching element
使用C#搜索xml文件的元素但获取以下内容
错误:序列不包含匹配元素
XNamespace siteNM = "http://schemas.microsoft.com/AspNet/SiteMap-File-1.0";
XDocument sitemap = new XDocument
(new XDeclaration("1.0", "UTF-8", null),
new XElement(siteNM + "siteMap",
new XElement(siteNM + "siteMapNode", new XAttribute("title", "Home"), new XAttribute("url", "home.aspx"), new XAttribute("description", "Home"))
));
XElement x = sitemap.Root;
我尝试了两种搜索元素的方法 ,但都给了我同样的错误 。
第一种方式:
XElement child = x.Descendants("siteMapNode").Where(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home").First();
第二种方式:
XElement child1 = x.Descendants("siteMapNode").First(el => (string)el.Attribute("title") == "Home");
请帮我。 非常感谢..
缺少命名空间
XElement child = x.Descendants(siteNM + "siteMapNode")
.First(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home");
你可能也应该在搜索查询中添加namespece:
XElement child = x.Descendants(siteNM + "siteMapNode").Where(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home").First();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.