?
Solved

XmlDocument selectSinglenodes?

Posted on 2004-03-22
5
Medium Priority
?
1,561 Views
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 ...

0
Comment
Question by:alain123
  • 3
  • 2
5 Comments
 
LVL 12

Expert Comment

by:esteban_felipe
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
www.estebanf.com
0
 
LVL 1

Author Comment

by:alain123
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?

thanks.
0
 
LVL 12

Expert Comment

by:esteban_felipe
ID: 10650032
yes :)
0
 
LVL 12

Accepted Solution

by:
esteban_felipe earned 1000 total points
ID: 10650044
Check out this code:

using System;
using System.Xml;

namespace ConsoleApplication20
{
      class Class1
      {
            [STAThread]
            static void Main(string[] args)
            {
                  XmlDocument xmlDoc = new XmlDocument();
                  xmlDoc.Load(@"C:\XMLFile1.xml");
                  XmlNode node = xmlDoc.SelectSingleNode("test/node");
                  testMethod(node);
                  Console.WriteLine(xmlDoc.InnerXml);
                  Console.Read();
            }
            public static void testMethod(XmlNode currentNode)
            {
                  currentNode.Attributes["id"].Value = "100";
            }
      }
}


The original xml is:

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

Compile it and check how it behaves.
0
 
LVL 1

Author Comment

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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month16 days, 5 hours left to enroll

850 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