473,385 Members | 1,752 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

SelectNodes into another XmlDocument?

Hi,

Is there an easier way to pull a subset of nodes from one XmlDocument to
another? I have the code below but would like to know if there is a more
streamlined method. Thanks, Dave

XmlNodeList nodeList = doc1.SelectNodes("//row");
foreach (XmlNode nodeCode in nodeList)
{
sb.Append(nodeCode.OuterXml);
}

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml("<root>" + sb.ToString() + "</root>");
return doc2.SelectSingleNode("root");
Nov 12 '05 #1
2 6166
HI,

"Dave" <Da**@discussions.microsoft.com> wrote in message news:21**********************************@microsof t.com...
Hi,

Is there an easier way to pull a subset of nodes from one XmlDocument to
another? I have the code below but would like to know if there is a more
streamlined method. Thanks, Dave

XmlNodeList nodeList = doc1.SelectNodes("//row");
foreach (XmlNode nodeCode in nodeList)
{
sb.Append(nodeCode.OuterXml);
MD: Here what is "sb"?
See, there is simple thing that if you want to get node from one XmlDocument and want to add or append in second XmlDocument then you need to import the node from first XmlDocument using "ImportNode" method.
May this suggestion help you,
The example is given at the end of this post, }

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml("<root>" + sb.ToString() + "</root>");
return doc2.SelectSingleNode("root");


The simplest way, but not good programming,
public XmlNode getRootNode()

{

XmlDocument doc1 = new XmlDocument();

doc1.LoadXml("<root><row>mahesh</row><row>devjibhai</row><row>dhola</row></root>");

XmlNodeList list = doc1.SelectNodes("//row");

String str=null;

foreach (XmlNode node in list)

{

str += node.OuterXml;

}

XmlDocument doc2 = new XmlDocument();

doc2.LoadXml("<root>"+str+"</root>");

MessageBox.Show(doc2.OuterXml);

return doc2.SelectSingleNode("//root");

}

My suggestion for the same thing:

public XmlNode getRootNode()

{

XmlDocument doc1 = new XmlDocument();

doc1.LoadXml("<root><row>mahesh</row><row>devjibhai</row><row>dhola</row></root>");

XmlDocument doc2 = new XmlDocument();

doc2.LoadXml("<root></root>");

XmlNodeList list = doc1.SelectNodes("//row");

foreach (XmlNode node in list)

{

doc2.DocumentElement.AppendChild(doc2.ImportNode(n ode, true));

}

MessageBox.Show(doc2.OuterXml);

return doc2.SelectSingleNode("//root");

}

}

HTH
Mahesh Devjibhai Dhola
"Empower yourself...."

Nov 12 '05 #2


Dave wrote:

Is there an easier way to pull a subset of nodes from one XmlDocument to
another? I have the code below but would like to know if there is a more
streamlined method. Thanks, Dave

XmlNodeList nodeList = doc1.SelectNodes("//row");
foreach (XmlNode nodeCode in nodeList)
{
sb.Append(nodeCode.OuterXml);
}

XmlDocument doc2 = new XmlDocument();
doc2.LoadXml("<root>" + sb.ToString() + "</root>");
return doc2.SelectSingleNode("root");


You should use
doc.ImportNode(node, true)
on each node found by SelectNodes and then insert the imported node as
needed.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Robert Mark Bram | last post by:
Hi All! In the code below, I am reading in an xhtml document and attempting to use selectNodes to find a <p id="rmb"> node.. But the result is: 2 - */* 0 - */p Can anyone suggest what I...
5
by: Alexander Gnauck | last post by:
Hello, i have problems with the Namespaces and the .Net XML Parser My XML looks like this: <query xmlns="jabber:iq:roster"> <item jid="srlee@localhost" name="srlee"...
8
by: e-mid | last post by:
i want to get childs of specific xml node. normally i use xmlNodeList fooList = myNode.SelectNodes("foo"); but in compactframework , there is no selectNodes() method or selectSingleNode()...
1
by: Riko Eksteen | last post by:
Hi I am writing an application that listens to the NodeInserted event, and if the event is fired it does a selectnodes xpath query on the XmlDocument. Only problem is, when I call...
2
by: Adrian Parker | last post by:
I have the following xml doc... <?xml version="1.0"?> <GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"> <Body> <StatusReport> <StatusRecord>...
2
by: Michael H | last post by:
Hello group, I have a some xml that looks like this below: <tuneRequests ct="3" xmlns:sql="urn:schemas-microsoft-com:xml-sql" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <tr...
16
by: PiotrKolodziej | last post by:
Hi I'am using SelectNodes method to find if node followed by string exists, and if so i going to select that node. Create doc i such a way: doc = new XmlDocument(); ...
0
by: Karel Kral | last post by:
Hello, I have created simple COM service in .NET. COM is accessed from another not NET application by late binding call. And the problem: If I call NetComClass.Hello from another .NET code, all...
1
by: Jazper | last post by:
hi i have a problem querying a simple atom-feed by xpath using the XmlDocument. i'm googleing for hours now and can't get a solution: --- Code --- XmlDocument doc = new XmlDocument();...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.