Ian Lawton wrote:
Hi, in a C# app, I have an XPathDocument that looks
similar to this:
<row uniqueid="4234" />
<row uniqueid="4365" />
<row uniqueid="3124" />
<row uniqueid="9879" />
<row uniqueid="1332" />
<row uniqueid="4322" />
I want to import it into an existing XmlDocument that
looks like this:
<root>
<rows>
</rows>
</root>
I think you can see where its meant to go. Ive tried a
CreateNavigator to select the nodes but then seem to have
no way to import them into the XmlDocument.
You need additional plumbing for doing that: XPathNavigator reader by Don Box
[1] and XmlNodeWriter by Chris Lovett [2]. The idea is to read XPathNavigator
by XmlReader and to write to XmlDocument by XmlWriter.
Here is an example:
namespace Test2 {
public class Test {
static void Main(string[] args) {
//Prepare XPathDocument
string xml =
@"<root>
<row uniqueid=""4234"" />
<row uniqueid=""4365"" />
<row uniqueid=""3124"" />
<row uniqueid=""9879"" />
<row uniqueid=""1332"" />
<row uniqueid=""4322"" />
</root>";
XPathDocument doc1 = new XPathDocument(new StringReader(xml));
//Prepare XmlDocument
XmlDocument doc2 = new XmlDocument();
XmlElement root = doc2.CreateElement("root");
XmlElement rows = doc2.CreateElement("rows");
doc2.AppendChild(root);
root.AppendChild(rows);
//Create XPathNavigator reader
XPathNavigatorReader reader = new
XPathNavigatorReader(doc1.CreateNavigator(),
doc1.CreateNavigator().NameTable);
//Create XmlNodeWriter to write to DOM
XmlNodeWriter writer = new XmlNodeWriter(rows, false);
writer.WriteNode(reader, false);
//That's all, folks
doc2.Save(Console.Out);
}
}
}
[1]
http://www.gotdotnet.com/team/dbox/d...4-09T12:06:02Z
[2]
http://www.gotdotnet.com/Community/U...5-1fc569cde11c
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel