Hello,

I am am trying to get a ListView to populate based on an xml file.
to acheive this I would like to use the DataTable.

this is my XML file:

<?xml version="1.0"?>
<main>

<person>
<id>1</id>
<name><first>Joe</first><last>Blow</last></name>
</person>

</main>

And the C# that I am using to read it:

public mainForm()
{
InitializeComponent();
listView.View = View.Details;
FillList();
}

public static DataTable GetPeople()
{
DataSet dsStore = new DataSet();
dsStore.ReadXml("example.xml");
return dsStore.Tables["person"];
}

private FillList()
{
listView.Items.Clear();

DataTable dtPeople = GetPeople();

listView.BeginUpdate();
foreach (DataRow dr in dtPeople.Rows)
{
ListViewItem listItem = new ListViewItem(dr["id"].ToString());
listItem.ImageIndex = 0;

listItem.SubItems.Add(dr["name"].ToString());
listView.Items.Add(listItem);
}

if (listView.Columns.Count == 0)
{
listView.Columns.Add("ID", 100, HorizontalAlignment.Left);
listView.Columns.Add("Name", 100, HorizontalAlignment.Left);
}
listView.EndUpdate();
}

Alright, all this works, that is, without the "SubItems" part.
and that is my question.

The error i get is: Column 'name' does not belong to table person. (this is regarding the highlighted code that is in red)

Now, how would i be able to extract the name as a whole? (like i am trying to do in the example)

And also how would i be able to extract first and last names seperately?

Thanks a million in advance.

Welcome NetGD.

ReadXML method create two DataTable instances. These two DataTable instances forms a relationship.

public static  DataTable GetPeople()
{
DataSet dsStore = new DataSet();
dsStore.ReadXml("example.xml");
// Test the count of tables and a relation
MessageBox.Show(dsStore.Tables.Count + "  "  +  dsStore.Relations[0].RelationName)

return dsStore.Tables["person"];
}
private void FillList()
        {
            listView.Items.Clear();

            DataTable dtPeople = GetPeople();

            listView.BeginUpdate();
            foreach (DataRow dr in dtPeople.Rows)
            {
                ListViewItem listItem = new ListViewItem(dr["id"].ToString());
                listItem.ImageIndex = 0;
                // Get child rows from a parent one.
                //  person_name is a name of relation between two
                //  tables.
                DataRow[] r = dr.GetChildRows("person_name");
                if (r.Length != 0)
                    listItem.SubItems.Add(r[0][0].ToString());
               listView.Items.Add(listItem);
            }

            if (listView.Columns.Count == 0)
            {
                listView.Columns.Add("ID", 100, HorizontalAlignment.Left);
                listView.Columns.Add("Name", 100, HorizontalAlignment.Left);
            }
            listView.EndUpdate();
        }
    }

Thank you so much adatapost!
that works great and i understand that there is a relationship concept now.

thanks again.

Thank you so much adatapost!
that works great and i understand that there is a relationship concept now.

thanks again.

Thanks.

If you solved your problem then mark this thread as "Solved"

I have one more question.

Now that I have been experimenting with this,
I ran into a problem when i had another nested table.

The problem is that when i try and read the relation location_mailing or location_billing
It does not even run the add subitem because it reads the DataRow as being empty.

How would I be able to read a nested xml element like that?

This is my new xml file:

<person>  
<id>shit</id>  

<name>
     <first>Joe</first>
     <last>Blow</last>
</name> 

<location>
     <mailing>
          <city>Someplace1</city>
     </mailing>

     <billing>
          <city>Somplace2</city>
     </billing>
</location>
</person>

And the C#:

public mainForm()
{
InitializeComponent();
listView.View = View.Details;
FillList();
}

public static DataTable GetPeople()
{
DataSet dsStore = new DataSet();
dsStore.ReadXml("example.xml");
return dsStore.Tables["person"];
}

private FillList()
{
listView.Items.Clear();

DataTable dtPeople = GetPeople();

listView.BeginUpdate();
foreach (DataRow dr in dtPeople.Rows)
{
ListViewItem listItem = new ListViewItem(dr["id"].ToString());
listItem.ImageIndex = 0;

DataRow[] r = dr.GetChildRows("location_mailing");
     if (r.Length != 0)
     {
          listItem.SubItems.Add(dr[0]["city"].ToString());
     }
listView.Items.Add(listItem);
}

if (listView.Columns.Count == 0)
{
listView.Columns.Add("ID", 100, HorizontalAlignment.Left);
listView.Columns.Add("mailing", 100, HorizontalAlignment.Left);
}
listView.EndUpdate();
}

thanks,

oops the line: listItem.SubItems.Add(dr[0]["city"].ToString());
is actually: listItem.SubItems.Add(r[0]["city"].ToString());

but that is not the solution,
thanks,

Try this:

foreach(DataRow r in dtPeople.Rows)
            {
                DataRow[] t = r.GetChildRows("person_location");
                if (t.Length != 0)
                {
                    DataRow[] y = t[0].GetChildRows("location_mailing");
                    if (y.Length != 0)
                    {
                        Console.WriteLine(y[0]["city"].ToString());
                    }
                }
            }
This question has already been answered. Start a new discussion instead.