#define useEscapes
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Management.Smo;

namespace UrnFormat
{
    class Program
    {
        static void Main(string[] args)
        {
            Urn urn1 = new Urn(
             @"urn:www-danal-com:Server[@Name='CANOPUS5']
             /Database[@Name='scratch']
             /Table[@Name='My Table']"
             );

            Urn urn2 = new Urn(
             "www-danal-com:Server[@Name='CANOPUS5']" +
             "/Database[@Name='scratch']" +
             "/Table[@Name='My Table']"
             );
            Urn urn3 = new Urn(
             "www-danal-com::Server[@Name='CANOPUS5']" +
             "/Database[@Name='scratch']" +
             "/Table[@Name='My Table']");
            Urn urn4 = new Urn(
             ":asdf:asdf:Server[@Name='CANOPUS5']" +
             "/Database[@Name='scratch']" +
             "/Table[@Name='My Table']");

            XPathExpression xpr = urn1.XPathExpression;
            for (int index = 0; index < xpr.Length; index++)
            {
                XPathExpressionBlock xb = urn1.XPathExpression[index];
                Console.WriteLine(xb.Name );
                Console.WriteLine("  " + xb.Filter.NodeType.GetTypeCode().ToString());
                foreach (object o in xb.FixedProperties)
                {
                    Console.WriteLine(o);
                }
            }


#if useEscapes
            String unsc = Urn.EscapeString("urn:foo:Server[@Name='CANOPUS5']" +
             "/Database[@Name='scratch']" +
             "/Table[@Name='My Table']");
            Console.WriteLine(unsc);

            Urn urnEsc = new Urn(unsc);
            Console.WriteLine(urnEsc.Value);
            
#endif
            Server server = new Server("CANOPUS5");
            Type ty = typeof(Database);
            Console.WriteLine(urn1.GetNameForType(ty.Name ));
            Table t = (Table)server.GetSmoObject(urn1);
            Console.WriteLine(t.RowCount);
            t = (Table)server.GetSmoObject(urn2);
            Console.WriteLine(t.RowCount);
#if useEscapes
            // next line throws exception
            t = (Table)server.GetSmoObject(unsc);
            Console.WriteLine(t.RowCount);
#endif

            
            // throws exception
            t = (Table)server.GetSmoObject(urn3);
            Console.WriteLine(t.RowCount);
            // throws exception
            t = (Table)server.GetSmoObject(urn4);
            Console.WriteLine(t.RowCount);
        }
    }
}
