Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old November 11th, 2005, 10:50 PM
Ian Lawton
Guest
 
Posts: n/a
Default 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
  #2  
Old November 11th, 2005, 10:51 PM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: Copy XPathDocument into an XmlDocument

Ian Lawton wrote:
[color=blue]
> 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.[/color]

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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles