简体   繁体   English

无法读取web.sitemap

[英]Could not read web.sitemap

I have been trying reading web.sitemap using LINQ but could not make it. 我一直在尝试使用LINQ读取web.sitemap,但未能成功。 Following is my web.sitemap 以下是我的web.sitemap

<?xml version="1.0" encoding="utf-8"?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0">
<siteMapNode url="" title="Root">
<siteMapNode url="" title="Manage">
  <siteMapNode url="~/Users.aspx" title="Users" name="10001"/>
  <siteMapNode url="~/Targets.aspx" title="Target" name="10002" />
  <siteMapNode url="~/Cases.aspx" title="Case" name="10003" />
  <siteMapNode url="~/GeoFence.aspx" title="Geofence" name= "10004" />
</siteMapNode>
<siteMapNode url="" title="Configure">
  <siteMapNode url="~/CellDirectory.aspx" title="Cell Directory" name="10005" />
  <siteMapNode url="~/WhiteList.aspx" title="White List"  name="10006"/>
  <siteMapNode url="" title="Options" />
</siteMapNode>
<siteMapNode url="" title="Locate">
  <siteMapNode url="~/FindNow.aspx" title="Find Now"  name="10007"/>
  <siteMapNode url="~/TrackNow.aspx" title="Track Now" name="10008" />
</siteMapNode>
<siteMapNode url="" title="Analyse">
  <siteMapNode url="~/Dashboard.aspx" title="Dashboard" name="10009" />
  <siteMapNode url="~/Search.aspx" title="History" name="10010" />
</siteMapNode>
</siteMapNode>
</siteMap>

Following is my code 以下是我的代码

XElement xelement2 = XElement.Load(Server.MapPath("~/web.sitemap"));
var urlDescList1 = xelement2.Descendants()                                   
    .Where(sel => (string)sel.Attribute("name").Value == "10001")
    .SelectMany(sel => sel.Elements())
    .Select(nd => new
    {
        title = nd.Attribute("title").Value,
        url = nd.Attribute("url").Value
    }).FirstOrDefault();

It gives me null value. 它给了我null值。 I need to read the node that has name attribute value = 10001 我需要读取名称属性值为10001的节点

I think your query is just slightly off. 我认为您的查询刚刚结束。 You need to ensure the element has the name attribute and SelectMany(sel => sel.Elements()) is unnecessary. 您需要确保元素具有名称属性,并且SelectMany(sel => sel.Elements())是不必要的。

XElement xelement2 = XElement.Load(Server.MapPath("~/web.sitemap"));
var urlDescList1 =  
    xelement2.Descendants()                                   
        .Where(sel => sel.Attribute("name") != null && sel.Attribute("name").Value == "10001")=
        .Select(nd => new
        {
            title = nd.Attribute("title").Value,
            url = nd.Attribute("url").Value
        })
        .FirstOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM