Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6201207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:30:40+00:00 2026-05-24T04:30:40+00:00

I have an xml sitemap structured like a document tree, such that it looks

  • 0

I have an xml sitemap structured like a document tree, such that it looks like this:

<Site>
<File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
<Folder name="FolderName">
    <Security>
        <Role>Admin</role>
    </Security>
    <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
    <Folder name="subFoler">
        <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
        <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
        <Folder>
            <File GUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx">FileName</file>
        </Folder>
    </Folder>
</Folder>

*Note that this is NOT my actually xml file. The actual xml file is too big to just show. Basically what you need to take away from this is that there are potentially ‘X’ amount of folders nested within each other, and at some point in those folders, there can be ‘X’ amount of files, as well as children folders.

Also, some folders are given security, which is inherited by everything in that folder (files, child folders, files within child folders, etc). I am trying to come up with a LINQ to XML query to get the security of a given file based on that files GUID, and it works fine for level-1 and level-2 files, but when I try to run the query on a file that is 3 folders deep, it fails and I get a nullreference exception. Here is the query I am using:

XDocument sitemap = XDocument.Load(HttpContext.Current.Server.MapPath("/.../sitemap.xml"));
        XElement fileFromMap =
            sitemap.Descendants("File").Where(
            file => file.Attribute("GUID").Value == guid).First();

        XElement currentFile = new XElement("File",
            fileFromMap.Value,
            fileFromMap.Ancestors("Folder").SelectMany(
                folder =>
                {
                    XElement security = folder.Element("Security");
                    return (security != null ? security.Elements("Role") : null);
                }));

*and credit where credit is due, I got this query here

The nullreference exception is happening in the declaration of the currentFile variable, and I’m not sure why. I’ve made sure that the Guids match up…and since fileFromMap is being declared correctly, I know that my file is being found. I assume what needs to be done here is something to better check parent folders recursively for security. The query can stop as soon as it finds any security, because the way the site is set up, there should be no conflicting security declarations. (eg, no folder that has defined security will be within a folder that already has defined security)

If I am wrong, and this isn’t what I need to do, please offer any suggestions you may have, and feel free to change the title of this question accordingly as to better document it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T04:30:41+00:00Added an answer on May 24, 2026 at 4:30 am

    (It would probably have been best if you left me a comment in your previous question instead, but I might as well answer this one now that it’s posted 😉

    There’s a mistake in the code I provided: I thought SelectMany() collapsed null items in the same way XElement.Add() does, and I was wrong. If there is a folder without roles in the ancestor chain, SelectMany() will throw a NullReferenceException.

    We only need to feed it an empty enumerable of XElement to solve the problem:

    XElement currentFile = new XElement("File",
        fileFromMap.Value,
        fileFromMap.Ancestors("Folder").SelectMany(
            folder =>
            {
                XElement security = folder.Element("Security");
                return (security != null
                        ? security.Elements("Role") : new XElement[0]);
            }));
    

    I’ll update my original answer with a backlink.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have xml that looks like this: <todo-list> <id type=integer>#{id}</id> <name>#{name}</name> <description>#{description}</description> <project-id
I have sitemap which looks like this: <?xml version=1.0 encoding=utf-8 ?> <siteMap xmlns=http://schemas.microsoft.com/AspNet/SiteMap-File-1.0 >
I have two XML file sitemap.xml and mouse.xml which look like below.Here the thing
I have an XML file (a sitemap using Google's <image:image> extensions) that I need
Greetings! If I have XML such as this: <Root> <AlphaSection> . . . </AlphaSection>
i have an xml file sitemap.xml as shown below ..i need to add one
I have an ASP.net site and I created a web.sitemap file in the root
I am trying to use JQuery to parse a sitemap.xml to look like this
I have XML files in a directory that I wish to get over to
I have XML with a value like the following: <products> <product id=1 name=All Products>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.