none
How to parse a ETL string RRS feed

  • Question

  • This is how I get the string from an ETL file. It appears like XML/JSON but it is neither

    <Event MSec= "14710.1843" PID="1820" PName=        "" TID="21844" EventName="ErrorEvent" ProviderName="MyProvider" ErrorMsg="XYZ: The memory was not enough to create an object " ErrorID="4,660\"/>

    Here I have made it definable in a C# program with "\" to avoid syntax error. 

    string Event1 = "<Event MSec= \"14710.1843\" PID=\"1820\" PName=        \"\" TID=\"21844\" EventName=\"ErrorEvent\" ProviderName=\"MyProvider\" ErrorMsg=\"XYZ: The memory was not enough to create an object \" ErrorID=\"4,660\"/>";

    If someone could kindly show me how to parse it or separate it to ID/Value pair as shown below that would be much appreciated. Thanks

    MSec=  "14710.1843"

    PID="1820"

    PName=        ""

    TID="21844"

    EventName="ErrorEvent"

    ProviderName="MyProvider"

    ErrorMsg="XYZ: The memory was not enough to create an object "

    ErrorID="4,660"

    Any help pointers are welcome. Thanks

    regards

    ananda


    Friday, May 17, 2019 3:15 AM

Answers

  • > I was hoping a JSON parser like solution where in you parse and get 2 strings the first
    > one being the ID second one being the value.

    That's exactly what you get with XDocument.Parse.  Viorel was just showing you an example fetching specific attributes, but that "root" object is an XElement that contains an Attributes collection.  You can troll through that collection to find all of the attribute/value pairs.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, May 21, 2019 7:28 AM
  • Viorel thanks a lot I added using System.Xml.Linq and it worked perfect. However earlier in the joy of seeing it work I forgot one main thing. The string I gave was just an example. The ETL file I get can have the ID as anything. Ex:

    <Event xyz= "14710.1843" PID="1820" abc=        "" def="21844" ghi="ErrorEvent" kjl="MyProvider" mno="XYZ: The memory was not enough to create an object " pqr="4,660\"/>

    So I cannot hardcode like string MSec = root.Attribute("Msec").Value

    I was hoping a JSON parser like solution where in you parse and get 2 strings the first one being the ID second one being the value. Keep on parsing till it hits the end. Then the algorithm is agnostic to the string recevied. Sorry I marked it not answred. Please suggest a parser kind of answer. Thanks 

    Friday, May 17, 2019 5:15 AM

All replies

  • Try this:

    var xdoc = XDocument.Parse( Event1 );
    var root = xdoc.Root;
    
    string MSec = root.Attribute( "MSec" ).Value;
    string PID = root.Attribute( "PID" ).Value;
    string PName = root.Attribute( "PName" ).Value;
    string TID = root.Attribute( "TID" ).Value;
    string EventName = root.Attribute( "EventName" ).Value;
    string ProviderName = root.Attribute( "ProviderName" ).Value;
    string ErrorMsg = root.Attribute( "ErrorMsg" ).Value;
    string ErrorID = root.Attribute( "ErrorID" ).Value;
    

    Friday, May 17, 2019 4:43 AM
  • Viorel thanks a lot I added using System.Xml.Linq and it worked perfect. However earlier in the joy of seeing it work I forgot one main thing. The string I gave was just an example. The ETL file I get can have the ID as anything. Ex:

    <Event xyz= "14710.1843" PID="1820" abc=        "" def="21844" ghi="ErrorEvent" kjl="MyProvider" mno="XYZ: The memory was not enough to create an object " pqr="4,660\"/>

    So I cannot hardcode like string MSec = root.Attribute("Msec").Value

    I was hoping a JSON parser like solution where in you parse and get 2 strings the first one being the ID second one being the value. Keep on parsing till it hits the end. Then the algorithm is agnostic to the string recevied. Sorry I marked it not answred. Please suggest a parser kind of answer. Thanks 

    Friday, May 17, 2019 5:15 AM
  • Hi ananda,

    Thank you for posting here.

    I think the following link is similar to your problem, you could have a look.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/e2c2f9b5-210e-457a-b2b4-7ff66e0620bb/how-do-i-extract-information-from-etl-files-in-net?forum=csharpgeneral​​​​​​​

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 17, 2019 7:50 AM
    Moderator
  • Sorry Jack I tried that and that will not work for me. In my case it has to be a programmatic fix. I was hoping something like the following JSON parser. Where I get the tuple neatly separate to an array. 

    thanks

    ananda


     for (int i = 0; i < list.Count; i++)
                    {
                        JObject parsed = JObject.Parse(abcd);
                        foreach (var pair in parsed)
                        {
                            Console.WriteLine("{0} : {1}", pair.Key, pair.Value);
                        }
                    }

    Friday, May 17, 2019 5:41 PM
  • Hi ananda,

    Thanks for the feedback.

    Based on my search, I do not find the way to read it directly using C#. You could convert .etl to .csv file and then read it. Then you could use the following code to parse a ETL file.

    https://stackoverflow.com/questions/3507498/reading-csv-files-using-c-sharp​​​​​​​

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, May 20, 2019 7:01 AM
    Moderator
  • > I was hoping a JSON parser like solution where in you parse and get 2 strings the first
    > one being the ID second one being the value.

    That's exactly what you get with XDocument.Parse.  Viorel was just showing you an example fetching specific attributes, but that "root" object is an XElement that contains an Attributes collection.  You can troll through that collection to find all of the attribute/value pairs.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Tuesday, May 21, 2019 7:28 AM
  • @Viorel kindly accept my profound apologies. I did not realize it and then I was busy with something else and just now checked. Tim thanks for the clarification. I got it fully working now and here is the fully working code. It just works perfect giving out both the key:value tuple

    string Event1 = "<Event MSec= \"14710.1843\" PID=\"1820\" PName=        \"\" TID=\"21844\" EventName=\"ErrorEvent\" ProviderName=\"MyProvider\" ErrorMsg=\"XYZ: The memory was not enough to create an object \" ErrorID=\"4,660\"/>";

           var xdoc = XDocument.Parse(Event1);
            var root = xdoc.Root;

            foreach (var node in xdoc.DescendantNodes()) 

            foreach (XElement element in xdoc.Descendants("Event"))
            {
                    foreach (XAttribute attrib in element.Attributes())
                    {
                        Console.WriteLine(String.Format("\t{0}={1}\n", attrib.Name, attrib.Value));
                }

            }

    Friday, May 24, 2019 5:01 AM