How to parse this string type string from SMO?

I am wondering if there is a standard way to parse a string like this:

Server[@Name='MyServerName']/Database[@Name='MyDatabaseName']/Table[@Name='MyTableName' and @Schema='MySchemaName']

The result should be:

  • ItemName (Server)
    • PropertyName (Name), PropertyValue (MyServerName)
  • ItemName (Database)
    • PropertyName (Name), PropertyValue (MyDatabaseName)
  • ItemName (Table)
    • PropertyName (Name), PropertyValue (MyTableName)
    • PropertyName (Schema), PropertyValue (MySchemaName)

The most obvious here is to create a regular expression (and, of course, String.Split), but could there be a better, standard way?

For information: the string comes from SMO Urn.Value.

UPDATE

The answer is found, see below.

+5
source share
4 answers

Ok, I did it. :)

. Urn.XPathExpression . Microsoft.SqlServer.Management.Sdk.Sfc.XPathExpression, System.Xml.XPath.XPathExpression ( ).

XPathExpression SMO :

        var urn = new Urn("Server[@Name='MyServerName']/Database[@Name='MyDatabaseName']/Table[@Name='MyTableName' and @Schema='MySchemaName']");

        for (var i = 0; i < urn.XPathExpression.Length; i++)
        {
            Console.WriteLine("ItemName ({0})", urn.XPathExpression[i].Name);

            foreach (DictionaryEntry item in urn.XPathExpression[i].FixedProperties)
            {
                Console.WriteLine("\tPropertyName ({0}), PropertyValue ({1})", item.Key, item.Value);
            }
        }

:

ItemName (Server)
        PropertyName (Name), PropertyValue ('MyServerName')
ItemName (Database)
        PropertyName (Name), PropertyValue ('MyDatabaseName')
ItemName (Table)
        PropertyName (Name), PropertyValue ('MyTableName')
        PropertyName (Schema), PropertyValue ('MySchemaName')

Christian.K, XPath!

+5

, -

var resultArr=str.Split(new char[]{'/', '[',']','@', '='}); , .

.

0

, .

-

    string input = @"Server[@Name='MyServerName']/Database[@Name='MyDatabaseName']/Table[@Name='MyTableName' and @Schema='MySchemaName']";
    string elementPattern = @"(?<ItemName>\w+)\[@(?<PropertyName>\w+)='(?<PropertyValue>\w+)'( and @(?<PropertyName>\w+)='(?<PropertyValue>\w+)')*\]";

    Regex elementRegex = new Regex(elementPattern, RegexOptions.Compiled);

    string[] elements = input.Split('/');

    foreach (string element in elements)
    {
        Match m = elementRegex.Match(element);

        if (m.Success)
        {
            Console.WriteLine("ItemName ({0})", m.Groups["ItemName"]);

            foreach (Capture capture in m.Groups["PropertyName"].Captures)
            {
                Console.WriteLine("PropertyName ({0})", capture.Value);
            }

            foreach (Capture capture in m.Groups["PropertyValue"].Captures)
            {
                Console.WriteLine("PropertyValue ({0})", capture.Value);
            }
        }
    }

    Console.ReadKey();

: -

ItemName (Server)
PropertyName (Name)
PropertyValue (MyServerName)
ItemName (Database)
PropertyName (Name)
PropertyValue (MyDatabaseName)
ItemName (Table)
PropertyName (Name)
PropertyName (Schema)
PropertyValue (MyTableName)
PropertyValue (MySchemaName)

: .

0
source

Maybe a little late, but I was looking for the same answer, and then I stumbled. Urn has several Get functions.

 var urn = new Urn("Server[@Name='MyServerName']/Database[@Name='MyDatabaseName']/Table[@Name='MyTableName' and @Schema='MySchemaName']");
    Console.WriteLine(urn.GetNameForType("Table"));

The result will be "MyTableName"

0
source

All Articles