none
Writing to a 2 dimensional string array RRS feed

  • Question

  • As you can see I am new to C# I tried many things it just would not work. Please suggest a fix for the the two lines with the comment. 

    using System; 
    using System.Xml.Linq;

    class Program
    {
        static void Main(string[] args)
        {
    string Event1 = "<Event MSec= \"14710.1843\" PID=\"1820\" PName=        \"\" TID=\"21844\" EventName=\"ErrorEvent\" ProviderName=\"Abcd\" ErrorMsg=\"The brown fox jumps over the white moon \" ErrorID=\"4,660\"/>";
    string[, ,] EventData = new string[100, 100, 1000];      // Array with 100 elements, string one 100 char length, string 2 1000 char length

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

    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));
    //I want to store attrib.Name, attrib.Value in a 2 dimensional string array
    EventData[i++] = {{attrib.Name, attrib.Value}};   // This does not work
    }
    }
    }
    }

    Thanks

    Friday, May 24, 2019 5:19 AM

Answers

  • Greetings Ananda.

    I'm not sure exactly what you are after, but I think it might be something like this.

    using System; 
    using System.Xml.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
    string Event1 = "<Event MSec= \"14710.1843\" PID=\"1820\" PName=        \"\" TID=\"21844\" EventName=\"ErrorEvent\" ProviderName=\"Abcd\" ErrorMsg=\"The brown fox jumps over the white moon \" ErrorID=\"4,660\"/>";
    string[,] EventData = new string[100, 2]; // Array of 200 strings, arranged in 100 rows of two columns (2 dimensional array).
    
    var xdoc = XDocument.Parse(Event1);
    var root = xdoc.Root;
    int i = 0;
    
    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));
    
    EventData[i, 0] = attrib.Name; // First column.
    EventData[i, 1] = attrib.Value; // Second column.
    i++;
    }
    }
    }
    } 

    Friday, May 24, 2019 5:29 AM
  • First of all, note that your array is not 2-dimensional but 3-dimensional. The dimensions are 100x100x1000, i.e., your array contains ten million elements.

    For two dimensions (100x2 strings) you would do this:

    string[,] EventData = new string[100, 2];      // Array with 200 elements arranged as 100 rows and 2 columns

    To store your values you would do this:

    EventData[i, 0] = attrib.Name;

    EventData[i++, 1] = attrib.Value;

    Note, however, that there are other containers that might be better than an array for storing this type of information. For instance, a Dictionary<string, string> or a List<XAttribute> might be much better than the array depending on how you intend to process this information.

    Friday, May 24, 2019 5:38 AM
    Moderator

All replies

  • Greetings Ananda.

    I'm not sure exactly what you are after, but I think it might be something like this.

    using System; 
    using System.Xml.Linq;
    
    class Program
    {
        static void Main(string[] args)
        {
    string Event1 = "<Event MSec= \"14710.1843\" PID=\"1820\" PName=        \"\" TID=\"21844\" EventName=\"ErrorEvent\" ProviderName=\"Abcd\" ErrorMsg=\"The brown fox jumps over the white moon \" ErrorID=\"4,660\"/>";
    string[,] EventData = new string[100, 2]; // Array of 200 strings, arranged in 100 rows of two columns (2 dimensional array).
    
    var xdoc = XDocument.Parse(Event1);
    var root = xdoc.Root;
    int i = 0;
    
    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));
    
    EventData[i, 0] = attrib.Name; // First column.
    EventData[i, 1] = attrib.Value; // Second column.
    i++;
    }
    }
    }
    } 

    Friday, May 24, 2019 5:29 AM
  • First of all, note that your array is not 2-dimensional but 3-dimensional. The dimensions are 100x100x1000, i.e., your array contains ten million elements.

    For two dimensions (100x2 strings) you would do this:

    string[,] EventData = new string[100, 2];      // Array with 200 elements arranged as 100 rows and 2 columns

    To store your values you would do this:

    EventData[i, 0] = attrib.Name;

    EventData[i++, 1] = attrib.Value;

    Note, however, that there are other containers that might be better than an array for storing this type of information. For instance, a Dictionary<string, string> or a List<XAttribute> might be much better than the array depending on how you intend to process this information.

    Friday, May 24, 2019 5:38 AM
    Moderator
  • Thank you Ante Meredian and Alberto Poblacion. It worked perfectly. I come from the C world so got mixed with my array dimensions sorry. Alberto thanks for introducing me to the Dictionary and List. As an experiment I tried all of them and Dictionary was what I had in mind. That ties into what I had thought off in C parlance.  

            string[,] EventData = new string[100, 2];
            List<XAttribute> EventXList = new List<XAttribute>();
            IList<string> EventDataList = new List<string>();
            var EventDataList1 = new List<string>() { "'', '' "};
            IDictionary<string, string> EventDataListdict = new Dictionary<string, string>();

    ..........................................................................................................

                       EventXList.Add(attrib);
                        EventDataList.Add(attrib.Name.ToString());
                        EventDataList.Add(attrib.Value.ToString());
                        EventDataList1.InsertRange(i, new string[] { attrib.Name.ToString(), attrib.Value.ToString() });
                        EventDataListdict.Add(attrib.Name.ToString(), attrib.Value.ToString());
                        EventData[i, 0] = attrib.Name.ToString(); // First column.
                        EventData[i, 1] = attrib.Value.ToString(); // Second column.
                        i++;

    Once again thanks to both of you I can start my day with the new code in hand :)

    Friday, May 24, 2019 2:05 PM