473,473 Members | 1,999 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Copy XPathDocument into an XmlDocument

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.

Any help appreciated.

Ian
Nov 11 '05 #1
1 4361
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

Nov 11 '05 #2

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

Similar topics

2
by: Robert Strickland | last post by:
I load an xml stream into a xpathdocument type, compile a xpath expression and select to specific node. I find the node and wish to return the innerxml of that node. When I use value of the current...
2
by: Jim Lewis | last post by:
After everything I have read and some of my own testing I am convinced that XPathDocument are more efficient if you are only using XML for read only and not modifying the XML. However, I have been...
2
by: Ed S | last post by:
Hi All, I'd like to convert from an XmlDocument to an XPathDocument (without saving the XmlDocument down to a file - this is a web app). I can't see any way to do this. Any help is...
5
by: Fahad Ashfaque | last post by:
Hi, I've an object of XmlDocument, I need to pass it to some function which takes XpathDocument object, How could I convert? There is not Conversion Operator overload like that. Your reply...
2
by: steve bull | last post by:
I have an XPathDocument which contains a list of color definitions. If I want to add a new color definition to, or delete an existing one from, the source xml file how can I force this to be...
5
by: Ben R. | last post by:
Hi, Could someone explain the functional difference between these classes? From what I understand, xpathdocument is faster in some scenarios, but I'm not sure why. Further, why is it that the...
2
by: srikar | last post by:
Hi, I have a XmlDocument object out of which I have to create the XPathDocument. How can I go about accomplishing this task? Any pointers are highly appreciated. Thanks, Srikar
2
by: Tony Girgenti | last post by:
Hello. I'm developing a web comsuming client using VS.NET 2003(VB), .NET Framework 1.1.4322, ASP.NET 1.1.4322, WSE2.0 on a WinXP Pro Sp2 computer. I'm getting an XML document this way: Dim...
5
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... I've been trying to improve the performance of our xml handling, so I've been doing some timing tests lately and ran across something I thought odd - XPathDocument seems slightly slower...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.