C#  •  Query Multiple Documents

Listing 2. The XQueryProcessor class provides a compilation and execution environment for XQuery statements. This allows you to run powerful queries against multiple XML documents, much as you can run SQL queries against relational database tables.

//Load query shown in Listing 3
StreamReader r =   new StreamReader(
   "../../Queries/BooksAuthors.xquery");

//Create XQuery processor
XQueryProcessor xp = new XQueryProcessor();

//Compile statement
xp.Compile(r.ReadToEnd());
r.Close();

//Define path to context documents and give alias
XmlDataSourceResolver ds = 
  new XmlDataSourceResolver(); 
ds.Add("BooksDoc",   "../../XML/Books.xml"); 
ds.Add("AuthorsDoc", "../../XML/Authors.xml");

//Perform query and write out results
StringWriter sw = new StringWriter();
xp.Execute(ds, sw);
Console.Write(sw.ToString());