using System;
using System.Diagnostics;
using DotnetPark.Commons.NDigester;
namespace OrdersParser{
/// <summary>
/// The main class of the application
/// </summary>
class MainClass
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
UseConfiguredDigester();
Console.WriteLine("Setting up Digester...");
// create Digester instance
Digester dig = new Digester();
// adding rules for orders parsing
// match for the "OrdersHistory"
// it will be our root object
dig.AddObjectCreate("ordersHistory", typeof(OrdersHistory));
// match for the "Order"
dig.AddObjectCreate("*/order", typeof(Order));
dig.AddSetNext("*/order", "AddOrder");
dig.AddSetProperties("*/order");
dig.AddPropertySetter("*/order/total", "Total");
// match for the "Client"
dig.AddObjectCreate("*/client", typeof(Client));
dig.AddSetProperties("*/client");
dig.AddPropertySetter("*/client/notes", "Notes");
dig.AddSetNext("*/client", "SetClient");
// match for the client "Address"
dig.AddObjectCreate("*/client/address", typeof(Address));
dig.AddSetProperties("*/client/address");
dig.AddSetNext("*/client/address", "SetAddress");
// match for the order "Item"
dig.AddObjectCreate("*/order/items/item", typeof(Item));
dig.AddSetProperties("*/order/items/item");
dig.AddSetNext("*/order/items/item", "AddItem");
// invoke parsing and returning objects tree
Console.WriteLine("Parsing file...");
OrdersHistory orders = (OrdersHistory)dig.Parse("Orders.xml");
Console.WriteLine("\nParsed orders:");
Console.WriteLine(orders.ToString());
}
public static void UseConfiguredDigester()
{
Digester dig = new Digester();
try
{
dig.Configure("OrdersConfig.xml");
}
catch(Exception e)
{
Debug.WriteLine(e.ToString());
}
OrdersHistory orders = (OrdersHistory)dig.Parse("Orders.xml");
Console.WriteLine("\nParsed orders:");
Console.WriteLine(orders.ToString());
}
}
}
|