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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:37:03+00:00 2026-05-12T11:37:03+00:00

I have an XmlDocument that is from a webservice, and I want to use

  • 0

I have an XmlDocument that is from a webservice, and I want to use a subset of the xml to populate a Gridview control. Unfortunately, it contains extra data that I don’t need. So I want to create a new XmlDocument from a subset of the existing xml document.

protected void Page_Load(object sender, EventArgs e)
{
   XmlDocument xmlDoc = Object.ReturnsXmlDocument;
   XmlDocument xmlDocResults = ???  //<results><result></result></results>
}

Basically, I want to create a new XmlDocument with the as the root element. Below is a shortened example of the original xml doc:

<?xml version="1.0" encoding="UTF-8"?>
<xml>
   <query_time>.12</query_time>
   <number_results>3</number_results>
   <results>
     <result><id>1</id></result>
     <result><id>2</id></result>
     <result><id>3</id> </result>
   </results>
</xml>

Anthony’s code helped point me in the right direction, but this is what actually worked for me.

XmlDocument xmlResults = new XmlDocument();
    XmlDeclaration xmlDec = xmlResults.CreateXmlDeclaration("1.0", "utf-8", null);
    XmlElement rootNode = xmlResults.CreateElement("results");
    xmlResults.InsertBefore(xmlDec, xmlResults.DocumentElement);
    xmlResults.AppendChild(rootNode);

    XmlNode node = xmlDoc.GetElementsByTagName("results")[0];

    xmlResults.GetElementsByTagName("results")[0].InnerXml = node.InnerXml.ToString();
  • 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-12T11:37:03+00:00Added an answer on May 12, 2026 at 11:37 am

    What you need is ImportNode:-

    XmlDocument xmlDoc = Object.ReturnsXmlDocument;
    XmlDocument xmlResults = new XmlDocument();
    xmlResults.AppendNode(xmlResults.ImportNode(xmlDoc.SelectSingleNode("/xml/results"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an XmlNodeList that contains packets (item) from the root of the XML
I have an XmlDocument that already exists and is read from a file. I
I have an XML document that I generate from an Entity Framework object. The
I have an XML document that includes XPath expressions that I need to use
I am developing a system that will receive a XML (XmlDocument) via webservice. I
I have an XMLDocument that I have read in from file. The file is
I have the following xml that's sent to me from a web service. I'm
Here's the background, I have a webservice from a vendor/partner that returns a large
I have a project that reads some data from xml file Convert.ToDouble metod works
I have a program that is generating Xml Files from data out of a

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.