0

I used a method from @yadavr (many thanks for that great solution) and question he answered for issue LINK.

This works great when you want to convert List and show it to a DataGridView. Method is:

public static class XElementExtensions 
{
    public static DataTable ToDataTable(this XElement element) 
   {
        DataSet ds = new DataSet();
        string rawXml = element.ToString();
        ds.ReadXml(new StringReader(rawXml));
        return ds.Tables[0];
    }
    
    public static DataTable ToDataTable(this IEnumerable<XElement> elements) 
    {
        return ToDataTable(new XElement("Root", elements));
    }
}

After this, I only added DataSet as source of DataGridView.

However, I have found one problem when in XElement there are two nodes with same name, but different namespaces. First of all, if all nodes have unique name (regardless of a namespace), all columns in DataGridView have names like name of nodes, but without namespace. So, instead of something like "cim:Location", it will just show "Location". Problem emerges once there are "cim:Location" and "dns:Location". In that case I get exception saying that I have ambiguous column names.

Is there any way to include namespaces in the column names ?
I tried to use different approaches, but nothing is working for me.

  • You will never get the ds.ReadXml() to work with your XML file. The results will create a fragmented dataset the will be useless. Once an xml had more than four descendants you end up with datatables that are fragmented and cannot be join together to look like the input xml file. So do not waste your time continuing this approach. Use a different xml library to parse the input xml. – jdweng May 24 '21 at 23:59
  • @jdweng, thanks for the replay. This came as a solution for representing XElement as a tabular view. In other words, to place list of XElements in DataGreedView withouth using a mapping between XElement and internal model, and it looks like there is no easy way to do so. – Davor Cacic May 25 '21 at 05:09
  • There may no be a solution that uses one instruction, but it can be done. I've done it lots of times. – jdweng May 25 '21 at 09:31
  • Did you post this here somewhere maybe? Or is there some article about it? – Davor Cacic May 25 '21 at 18:18
  • The ReadXml Method create a dataset the root tag name is the dataset name. The next level tags are the names of the datatables. Next level tags are the column names for the table. And finally the next level tags are the row data. If you get more than the four level tags more datatables are created. So instead of the data being in one table you get multiple tables and no way of combining the table together. Try manually create an xml file and then read results and you will see what I mean. – jdweng May 25 '21 at 18:53
  • Ok, understood. Thanks for providing the explanation. At least, now i know that i should leave the concept. it was working good in the most cases. Too bad it doesn't include the namespace at all. I have only two levels, always, but not heaving namespaces creates it unusable. Thank you once again, @jdweng. – Davor Cacic May 28 '21 at 08:44

0 Answers0