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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:07:21+00:00 2026-06-06T03:07:21+00:00

For a xml Document like below I display all subfolders and files for the

  • 0

For a xml Document like below I display all subfolders and files for the currently selected folder. For that from the xml string i remove the unmatched folders and built a UI using XSLT on the front end. Seems the operation is removing all the nodes that makes resulting string invalid xml. so

  • How do i remove elements from xml using Linq to Xml without changing the validity of the document

Xml Document

<?xml version="1.0" encoding="utf-8"?>
<Folder>
  <Folders>
    <Folder ID="1" Name="Root" ParentId="0">
      <Files></Files>
    </Folder>
    <Folder ID="2" Name="My Documents" ParentId="1">
      <Files>
        <File Name="LicenceCode.txt" Size="2000" CreatedOn="1/1/2012 12:12:00 PM" CreatedBy="1" ModifiedOn="1/10/2012 10:12:56 AM" ModifiedBy="2"></File>
      </Files>
    </Folder>
  </Folders>
</Folder>

Code

XElement filesAndFolders = XElement.Parse(xmlDocumentString);
string outputFolders = string.concat(from folders in filesAndFolders in filesAndFolder.Elements("Folder").Folder("Folders") where folders.Attribute("ParentId").Value.Equals(selectedFolderId) select folders);
//pass outputFolders string to xsl to build the UI

Problem

The outputFolders string is invalid as it contains only below string not a valid document

<Folder ID="2" Name="My Documents" ParentId="1">
  <Files>
    <File Name="LicenceCode.txt" Size="2000" CreatedOn="1/1/2012 12:12:00 PM" CreatedBy="1" ModifiedOn="1/10/2012 10:12:56 AM" ModifiedBy="2"></File>
  </Files>
</Folder>
  • 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-06T03:07:22+00:00Added an answer on June 6, 2026 at 3:07 am

    How about removing the elements you don’t want:

            var doc = XDocument.Parse(xmlString);
    
            doc.Element("Folder").Element("Folders").Elements("Folder").Where(f => f.Attribute("ID").Value == "1").Remove();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an xml document like the below one that I am trying to
I have an xml document that looks something like below (resources.xml), with a corresponding
I have an xml document that looks like this. <foo> <bar type=artist/> Bob Marley
I'm trying deserialize a xml document that looks like this <invoices> <invoice>2848</invoice> <invoice>2849</invoice> <invoice>2850</invoice>
Hi I'm reading in an XML document like below <?xml version=1.0 encoding=utf-8?> <ArrayOfSYMBOL xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
I have an XML document that looks like this: <?xml version=1.0 encoding=UTF-8?> <xs:msgdata xmlns:xs=http://www.myCompany.com
I have a document that it's structure is like below. There are a lot
I have a XML document like shown below: <root> <level1> <level2> <level3> ..... </level3>
I have some xml XML Document with duplicate tag names like below :: <ROOT>
I have an XML document like this: <xml> <item> <title>Article 1</title> <text><![CDATA[Lorem ipsum dolor

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.