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...."