Saxon allows you to use the collection() function to access all the files in
a directory, for example
collection("file:///c:/some/dir?select=*.xml")
returns all the .xml files in the given folder.
So you can merge them like this:
or
There are a variety of ways to accomplish this with the Microsoft® .NET Framework, each of which uses a different type of XML API. To do this with the DOM API, you need to use the ImportNode function for copying XmlNode objects between documents. Since ImportNode only transfers control of the object between documents, you still need to call AppendChild (or InsertBefore, and so forth) in order to inject the node into the tree.
A There are a variety of ways to accomplish this with the Microsoft® .NET Framework, each of which uses a different type of XML API. To do this with the DOM API, you need to use the ImportNode function for copying XmlNode objects between documents. Since ImportNode only transfers control of the object between documents, you still need to call AppendChild (or InsertBefore, and so forth) in order to inject the node into the tree.
Figure 1 shows a complete C# example that merges two XML documents (product.xml and material.xml) into one. The new document has a root Container element which will hold the contents of the other two files. Use the XmlReader and XmlWriter APIs. To do this you'd use a single XmlWriter to generate the new XML document, along with several XmlReaders to merge other documents into the new one. XmlWriter provides a WriteNode method that reads through the supplied XmlReader object and writes all of its nodes in one shot (see Figure 2 ).
Figure 2 WriteNode Method
// new document stream
XmlWriter tw = new XmlTextWriter(Console.Out);
tw.WriteStartDocument();
tw.WriteStartElement("Container");
// write product.xml to new document stream
XmlTextReader reader = new XmlTextReader("product.xml");
tw.WriteNode(reader, true);
// write material.xml to new document stream
reader = new XmlTextReader("material.xml");
tw.WriteNode(reader, true);
tw.WriteEndElement();
tw.WriteEndDocument();
Figure 1 Merging XML Docs
// the other documents will be merged into this one
// under the Container element
XmlDocument doc = new XmlDocument();
XmlNode cont = doc.CreateElement("Container");
doc.AppendChild(cont);
// first document to merge
XmlDocument stuff = new XmlDocument();
stuff.Load("product.xml");
XmlNode imported =
doc.ImportNode(stuff.DocumentElement, true);
doc.DocumentElement.AppendChild(imported);
// second document to merge
stuff.Load("material.xml");
imported = doc.ImportNode(stuff.DocumentElement, true);
doc.DocumentElement.AppendChild(imported);
// print merged documents
Console.WriteLine(doc.InnerXml);
And finally, you could also use XSLT to accomplish this via the document function. This is probably the easiest solution of all:
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">