 |
/*
* Berkeley DB XML .NET API
*
* Copyright (C) 2007 Jana Business Consulting Ltd. All rights reserved.
*
* For more information, see http://www.janabiz.com.
*
*/
using System;
using JanaBiz.Db;
using JanaBiz.DbXml;
using JanaBiz.DbXml.Exceptions;
public class SimpleXmlEventWriter
{
private static string theContainer = "namespaceExampleData.dbxml";
private static void eventWriteDocument(XmlContainer container, XmlManager mgr, XmlTransaction txn,XmlUpdateContext uc)
{
XmlDocument doc = mgr.CreateDocument();
XmlDocumentConfig config=new XmlDocumentConfig();
config.GenerateName = true;
XmlEventWriter writer = container.PutDocumentAsEventWriter(txn,doc, uc,config);
writer.WriteStartDocument("1.0", "utf-8", null);
writer.WriteStartElement("a", null, null, 0, false);
writer.WriteStartElement("b", null, null, 2, false);
writer.WriteAttribute("a1", null, null, "one", true);
writer.WriteAttribute("b2", null, null, "two", true);
writer.WriteText(XmlEventType.Characters, "b node text");
writer.WriteEndElement("b", null, null);
writer.WriteStartElement("c", null, null, 0, false);
writer.WriteText(XmlEventType.Characters, "c node text");
writer.WriteEndElement("c", null, null);
writer.WriteEndElement("a", null, null);
writer.WriteEndDocument();
writer.Close();
string query = "collection('" + theContainer +"')/a";
XmlQueryContext context = mgr.CreateQueryContext();
XmlResults result = mgr.Query(txn,query, context);
Console.WriteLine("The follewing is the xmlcontents that is writed into the DBXmlContainer:");
while(result.MoveNext())
{
XmlValue xv = result.Current;
Console.WriteLine(xv.ToString());
}
}
public static void Main(string[] args)
{
string envdir = parseArguments(args);
DbEnvironment dbEnv = null;
XmlManager mgr = null;
XmlContainer container = null;
try
{
// Open an environment
dbEnv = CreateDbEnvironment(envdir);
// Open a manager
using (mgr = CreateManager(dbEnv))
{
// Open a transactional container
XmlContainerConfig containerconfig = new XmlContainerConfig();
containerconfig.Transactional = true;
using (container = mgr.OpenContainer(null, theContainer,
containerconfig))
{
// Start a transaction
using (XmlTransaction txn = mgr.CreateTransaction())
{
// Create an update context
using (XmlUpdateContext uc = mgr.CreateUpdateContext())
{
eventWriteDocument(container,mgr,txn,uc);
// Commit the event writer.
txn.Commit();
}
}
}
}
}
catch (DbXmlException e)
{
Console.WriteLine("Error writer document to container " + theContainer);
Console.WriteLine(e.ToString());
}
finally
{
if (container != null)
{
container.Close();
}
if (mgr != null)
{
mgr.Close();
}
if (dbEnv != null)
{
dbEnv.Close();
}
}
}
public static DbEnvironment CreateDbEnvironment(string envdir)
{
DbEnvironmentConfig envconf = new DbEnvironmentConfig();
envconf.CacheSize = 50*1024;
envconf.AllowCreate = true;
envconf.InitializeCache = true;
envconf.Transactional = true;
envconf.InitializeLocking = true;
envconf.InitializeLogging = true;
envconf.RunRecovery = true;
envconf.ErrorWriter = Console.Out;
envconf.ErrorPrefix = "LOG";
XmlManagerConfig mgrconfig = new XmlManagerConfig();
mgrconfig.AdoptEnvironment = true;
DbEnvironment env = new DbEnvironment(envdir, envconf);
return env;
}
public static XmlManager CreateManager(DbEnvironment env)
{
XmlManagerConfig mgrconfig = new XmlManagerConfig();
mgrconfig.AdoptEnvironment = true;
try
{
return new XmlManager(env, mgrconfig);
}
catch (Exception e)
{
env.Close();
throw e;
}
}
private static void Usage()
{
Console.WriteLine("This program adds several indexes to a DBXML container.");
Console.WriteLine("You should run exampleLoadContainer before running this example.");
Console.WriteLine("You are only required to pass this command the path location of the database");
Console.WriteLine("environment that you specified when you loaded the examples data:");
Console.WriteLine();
Console.WriteLine("\t-h <dbenv directory>");
Console.WriteLine("For example:");
Console.WriteLine("\taddIndex.exe -h examplesEnvironment");
Environment.Exit(-1);
}
private static string parseArguments(string[] args)
{
string envdir = null;
for (int i = 0; i < args.Length; ++i)
{
string arg = args[i];
if ((arg.StartsWith("-")
#if WIN32
|| arg.StartsWith("/")
#endif
) && arg.Length > 1)
{
switch (arg[1])
{
case 'h':
{
++i;
if (i >= args.Length)
{
Console.WriteLine("Invalid option: " + arg);
Usage();
}
envdir = args[i];
break;
}
default:
{
Console.WriteLine("Unknown option: " + arg);
Usage();
break;
}
}
}
else
{
Console.WriteLine("Too many arguments: " + arg);
Usage();
}
}
if (envdir == null)
{
Console.WriteLine("Environment directory not found.");
Usage();
}
return envdir;
}
}
 |
|
 |
|