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 8051001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:12:39+00:00 2026-06-05T07:12:39+00:00

I have an XML file that will be similar to the structure below: <?xml

  • 0

I have an XML file that will be similar to the structure below:

<?xml version="1.0" encoding="utf-8"?>
<Root Attr1="Foo" Name="MyName" Attr2="Bar" >
    <Parent1 Name="IS">
        <Child1 Name="Kronos1">
            <GrandChild1 Name="Word_1"/>
            <GrandChild2 Name="Word_2"/>
            <GrandChild3 Name="Word_3"/>
            <GrandChild4 Name="Word_4"/>
        </Child1>
        <Child2 Name="Kronos2">
            <GrandChild1 Name="Word_1"/>
            <GrandChild2 Name="Word_2"/>
            <GrandChild3 Name="Word_3"/>
            <GrandChild4 Name="Word_4"/>
        </Child2>
    </Parent1>
</Root>

The elements are not defined in that they can have different values than other files. What I do know is the “Name” attribute of each element before hand, which will always be defined. I need to be able to manipulate, and/or delete data within a selected element based on that name. For Example: removeElement("MyName.IS.Kronos1.Word_1") would delete the GrandChild1 element underneath the Child1 Parent.

My issues is that while using LINQ to XML queries I’m not able to select that element properly. Using this:

private IEnumerable<XElement> findElements(IEnumerable<XElement> docElements, string[] names)
{
    // the string[] is an array from the desired element to be removed.
    // i.e. My.Name.IS ==> array[ "My, "Name", "IS"]
    IEnumerable<XElement> currentSelection = docElements.Descendants();

    foreach (string name in names)
    {
        currentSelection =
            from el in currentSelection
            where el.Attribute("Name").Value == name
            select el;
    }

    return currentSelection;

}

To find where I need to remove the elements yields this result:

<?xml version="1.0" encoding="utf-8"?>
<Root Attr1="Foo" Name="MyName" Attr2="Bar" >
    <Parent1 Name="IS">
        <Child1 Name="Kronos1">
            <GrandChild2 Name="Word_2"/>
            <GrandChild3 Name="Word_3"/>
            <GrandChild4 Name="Word_4"/>
        </Child1>
        <Child2 Name="Kronos2">
            <GrandChild2 Name="Word_2"/>
            <GrandChild3 Name="Word_3"/>
            <GrandChild4 Name="Word_4"/>
        </Child2>
    </Parent1>
</Root>

After debugging it appears that all I’m doing is searching for the same document over again, but for different names each time. How do I search and select a specific element based on multiple parent attribute Names?

It should be noted, that the size of the XML (meaning levels of elements) are also variable. Meaning that there can as little as 2 levels (Parents) or up to 6 levels (Great-Great-GrandChildren). However, I NEED to be able to look at the root node’s Name Attribute as well.

  • 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-06-05T07:12:40+00:00Added an answer on June 5, 2026 at 7:12 am

    This should work:

    if (doc.Root.Attribute("Name").Value != names.First())
        throw new InvalidOperationException("Sequence contains no matching element.");
    
    var selection = doc.Root;
    
    foreach (var next in names.Skip(1))
        selection = selection.Elements().First(x => x.Attribute("Name").Value == next);
    
    return selection;
    

    You can replace the latest lines by the following if you want to:

    var selection = names.Skip(1).Aggregate(doc.Root, (current, next) => current.Elements().First(x => x.Attribute("Name").Value == next));
    

    The .First() method throws an exception if no matching element is found in source.


    The cleanest approach would be to add a new function:

    XElement SelectChildElement(XElement current, string child)
    {
        if (current == null)
            return null;        
    
        var elements = current.Elements();
        return elements.FirstOrDefault(x => x.Attribute("Name").Value == child);
    }
    

    Such that you can simply use it as following:

    if (doc.Root.Attribute("Name").Value != names.First())
        return null;
    
    return names.Skip(1).Aggregate(doc.Root, SelectChildElement);
    

    And then, if you ever need to select one child, you have a handy SelectChildElement() avaialble. If you want to do myElement.SelectChild(child) instead, you can call it from an extension.

    Also, as you use FirstOrDefault here, you don’t get an exception but get null returned instead.

    This way, it doesn’t have to keep track of exceptions which is often more costly…

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

Sidebar

Related Questions

I have an XML file that looks like <?xml version='1.0' encoding='UTF-8'?> <root> <node name=foo1
i have xml file that looks like this: <?xml version=1.0 encoding=UTF-8?> <!DOCTYPE pointList SYSTEM
Just a quick check really. I have an XML file that I will be
I have a string in an XML file that looks similar to this: M:Namespace.Class.Method(Something
I have an xml file like below which I will use to set background
I have an xml file that contains a list of items i can deserialize
I have an XML file that has a number of nodes, each of which
I have an XML file that is very long, but here is a shot
I have an XML file that lists a series of items, and the items
I have an XML file that I'm trying to serialize into an object. Some

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.