XmlDocument selectSinglenodes?

Posted on 2004-03-22
Last Modified: 2007-12-19
how do i selectSinglenodes from an XmlDocument  without putting them into a XmlNode object?    in other words, i want to select single nodes into the same xmlDocument object ...

Question by:alain123
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
LVL 12

Expert Comment

ID: 10649825
Hi alain123,

I think you are missing something here. It seems to me that you think than the XmlNode is an "orphan" node and not part of the xmldocument.

selectSingleNode will return the first matching node. You keep the reference to that node using an XMLNode instance. But it's a reference. Any changes that you apply to the node will reflect in the XMLDocument instance.

So you are not "putting" it in a XmlNode, you are referencing it using a Xmlnode.

Esteban Felipe

Author Comment

ID: 10649946
Hi Esteban how is it going, so on your answer... " So you are not "putting" it in a XmlNode, you are referencing "it" using a Xmlnode." ==> do you mean it's referencing the xmlDocument?  and if I pass this object as a parameter to another class, would the changes afect the original xmldoc?

LVL 12

Expert Comment

ID: 10650032
yes :)
LVL 12

Accepted Solution

esteban_felipe earned 250 total points
ID: 10650044
Check out this code:

using System;
using System.Xml;

namespace ConsoleApplication20
      class Class1
            static void Main(string[] args)
                  XmlDocument xmlDoc = new XmlDocument();
                  XmlNode node = xmlDoc.SelectSingleNode("test/node");
            public static void testMethod(XmlNode currentNode)
                  currentNode.Attributes["id"].Value = "100";

The original xml is:

<?xml version="1.0" encoding="utf-8" ?>
      <node id="1" date="2004-04-05T12:20:00">
            node 1
      <node id="2" date="2004-01-12T12:20:00">
            node 2
      <node id="3" date="2004-02-03T12:20:00">
            node 3
      <node id="4" date="2004-03-10T12:20:00">
            node 4
      <node id="5" date="2004-06-11T12:20:00">
            node 5
      <node id="6" date="2004-06-01T12:20:00">
            node 6
      <node id="7" date="2004-08-02T12:20:00">
            node 7
      <node id="8" date="2004-10-04T12:20:00">
            node 8
      <node id="9" date="2004-12-04T12:20:00">
            node 9
      <node id="10" date="2004-01-04T12:20:00">
            node 10

Compile it and check how it behaves.

Author Comment

ID: 10650592
pretty nice example.. thank you.. i guess the xmldoc gets passed by value..

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question