XDocument,XElement:Sequence不包含匹配的元素(XDocument, XElement : Sequence contains no matching element)

编程入门 行业动态 更新时间:2024-10-23 20:26:49
XDocument,XElement:Sequence不包含匹配的元素(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");

请帮帮我。 非常感谢..

Searching element of xml file using C# but getting following

Error: Sequence contains no matching element

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;

I have tried following two methods for searching element but both give me same error.

1st way:

XElement child = x.Descendants("siteMapNode").Where(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home").First();

2nd Way:

XElement child1 = x.Descendants("siteMapNode").First(el => (string)el.Attribute("title") == "Home");

please help me. Thank you so much..

最满意答案

缺少名称空间

XElement child = x.Descendants(siteNM + "siteMapNode") .First(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home");

missing namespace

XElement child = x.Descendants(siteNM + "siteMapNode") .First(el => el.Attribute("title") != null && el.Attribute("title").Value == "Home");

更多推荐

本文发布于:2023-07-27 17:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293588.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不包含   元素   XElement   XDocument   Sequence

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!