Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XSLT to merge two XML documents using C#

858 views
Skip to first unread message

James W

unread,
Dec 1, 2003, 11:56:49 AM12/1/03
to
The problem:

I have two pieces of XML that I need to transform into one
piece of XML using XSLT and C#. Each piece of XML has a
completely different structure, so the merge would be more
of a concatanation.

I have found examples of merging XML files, but none in
C#. I would like to do it in one pass, but it maybe a
case of doing two merges...I just don't know.

Is there a way of doing it in one pass? A small code
snippet would be appreciated.

Thanks in advance,

James.

Oleg Tkachenko

unread,
Dec 1, 2003, 12:07:56 PM12/1/03
to
James W wrote:

> I have two pieces of XML that I need to transform into one
> piece of XML using XSLT and C#. Each piece of XML has a
> completely different structure, so the merge would be more
> of a concatanation.
>
> I have found examples of merging XML files, but none in
> C#. I would like to do it in one pass, but it maybe a
> case of doing two merges...I just don't know.

So you wanna C# or XSLT?
btw, check out recently published article at xml.com on the topic:
http://www.xml.com/pub/a/2003/11/26/q-and-a.html
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

James W

unread,
Dec 2, 2003, 3:37:57 AM12/2/03
to
I checked out that example which was good, thanks. I
would like to do that in C#...the files wont be saved on
disk, they'll be in memory, as a string or stream,
whatever...

Thanks

Oleg Tkachenko

unread,
Dec 2, 2003, 8:01:13 AM12/2/03
to
James W wrote:
> I checked out that example which was good, thanks. I
> would like to do that in C#...the files wont be saved on
> disk, they'll be in memory, as a string or stream,

You can run XSLT in C# actually. Input documents might be string,
stream, XmlReader, files, whatever. The same goes for result.

anon...@discussions.microsoft.com

unread,
Dec 2, 2003, 8:14:08 AM12/2/03
to
I know you can do it, I already have C# code that does
it...I refer back to my original question: I have 2
different XML files, that I want to transform using a
single XSLT in ONE pass. Is this possible? And how?

>.
>

Oleg Tkachenko

unread,
Dec 2, 2003, 8:36:22 AM12/2/03
to
anon...@discussions.microsoft.com wrote:

> I know you can do it, I already have C# code that does
> it...I refer back to my original question: I have 2
> different XML files, that I want to transform using a
> single XSLT in ONE pass. Is this possible? And how?

Well, that's exactly what xml.com article I mentioned shows. Just open
both documents via document() function and apply your merge logic.

anon...@discussions.microsoft.com

unread,
Dec 2, 2003, 11:00:30 AM12/2/03
to
Document() function on what object? Also, the xml will be
in memory, not on disk. Can this function cope with
strings or streams?

>.
>

Chris Barber

unread,
Dec 2, 2003, 11:07:30 AM12/2/03
to
Not a very slick method but just create the following as an XML document (in
memory of course):

<root document="Two documents to merge">
<document1>
'Contains document 1
</document1>
<document2>
'Contains document 2
</document2>
</root>

and apply your XSLT.

Alternatively pass in the string data as a parameter of the transform? .....
yaaargh - I have no idea if that can be used at all - maybe Oleg, Dimitre,
Marrow can make a suggestion about that.

Chris.

<anon...@discussions.microsoft.com> wrote in message
news:00a801c3b8ed$691994f0$a101...@phx.gbl...

anon...@discussions.microsoft.com

unread,
Dec 2, 2003, 11:09:27 AM12/2/03
to
In fact, let me be more clear:

I have two XML streams, and one XSLT in C#. I need to
merge/concatanate and transform the two XML streams into a
single XML stream. None of these streams can be persisted
to disk. Therefore the xsl:document function would not
appear to be relevant.

Is it possible to do this in C# - in one pass - using a
XslTransform object?

Thanks,

James

Oleg Tkachenko

unread,
Dec 2, 2003, 11:30:04 AM12/2/03
to
anon...@discussions.microsoft.com wrote:

> I have two XML streams, and one XSLT in C#. I need to
> merge/concatanate and transform the two XML streams into a
> single XML stream. None of these streams can be persisted
> to disk. Therefore the xsl:document function would not
> appear to be relevant.

That makes difference. I see couple of approaches here. First, standard
facility for custom XML input sources is XmlResolver. You can implement
custom XmlResolver class, which returns Stream object and call it in
document() function. XslTransform supports only Stream and
XPathNaigator, no XmlReader unfortunately.
Alternatively you can represent both streams as single XML somehow, say
via custom XmlReaderReader, that's going to be more effective if you
have XmlReader, not Stream objects.

Robert Rossney

unread,
Dec 2, 2003, 1:22:57 PM12/2/03
to
<anon...@discussions.microsoft.com> wrote in message
news:000d01c3b8ee$a93b5130$a301...@phx.gbl...
: In fact, let me be more clear:

No. You can't pass two streams to an XslTransform. I believe that it is
also not possible to pass a node-set parameter to an XslTransform, which
would be the other way to make the XSLT transform process both streams in a
single pass.

I believe that the fastest way to do what you're trying to do would be to
pull the streams into two XmlDocument objects, and use ImportNode to combine
them into one:

XmlNode newNode = doc1.ImportNode(doc2.DocumentElement);
doc1.DocumentElement.AppendChild(newNode);

Then apply the transform to the resulting document.

Bob Rossney
r...@well.com


Robert Rossney

unread,
Dec 2, 2003, 1:34:50 PM12/2/03
to
"Robert Rossney" <r...@well.com> wrote in message
news:eOBsUFQu...@tk2msftngp13.phx.gbl...
: <anon...@discussions.microsoft.com> wrote in message

: news:000d01c3b8ee$a93b5130$a301...@phx.gbl...
: : In fact, let me be more clear:
: :
: : I have two XML streams, and one XSLT in C#. I need to
: : merge/concatanate and transform the two XML streams into a
: : single XML stream. None of these streams can be persisted
: : to disk. Therefore the xsl:document function would not
: : appear to be relevant.
: :
: : Is it possible to do this in C# - in one pass - using a
: : XslTransform object?
:
: No. You can't pass two streams to an XslTransform. I believe that it is
: also not possible to pass a node-set parameter to an XslTransform, which
: would be the other way to make the XSLT transform process both streams in
a
: single pass.

While I'm right that you can't pass a node-set parameter to an XslTransform,
you *can* pass a result-tree-fragment parameter, and then use the extension
function msxsl:node-set() to convert it to a node set. I'd bet that this is
no faster than pulling both streams into a single source document (and it's
certainly going to be harder to code), but it's probably worth checking out
and benchmarking if performance is critical.

Bob Rossney
r...@well.com


Oleg Tkachenko

unread,
Dec 2, 2003, 1:41:31 PM12/2/03
to
Robert Rossney wrote:

> No. You can't pass two streams to an XslTransform. I believe that it is
> also not possible to pass a node-set parameter to an XslTransform, which
> would be the other way to make the XSLT transform process both streams in a
> single pass.

Why? Take a look at XsltArgumentList class. It allows to pass strings,
booleans, numbers, rtfs and nodesets parameters. So James can load
second stream into XPathDocument and then pass it as parameter (in
XPathNodeIterator form though) to the transformation.

James W

unread,
Dec 3, 2003, 3:41:59 AM12/3/03
to
Thanks guys, you have given me a lot to get on with.

I will be back if I get stuck!

James

0 new messages