467,118 Members | 999 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,118 developers. It's quick & easy.

Modify an XML File with a DataSet and Child Relations

Hello,

I want to modify an existing XML-File based on Input in a Form.
I used a DataSet to load the XML File with the ReadXml method.

I then displayed the relevant entries via the GetChildRows method.

I now want to change/add/delete those ChildRows:

How can I accomplish this?

Here is the code I used to modify the Dataset, but it is not working
yet:
DataTable tbSelections = dsConfig.Tables["selector"];
DataTable tbItems = dsConfig.Tables["item"];

foreach (DataRow row in tbSelections.Rows)
{
if (cb_selector.SelectedItem.ToString() ==
row["name"].ToString())
{
string[] lines = tb_selectoritem.Lines;
DataRow[] rowstemp =
row.GetChildRows("selector_item");
DataRow[] rows;
// check if we more or less entries than before, so that we know we
have to add or delete or just change entries
if(lines.Length!=rowstemp.Length)
{
rows=new DataRow[lines.Length];
}
else
{
rows=new DataRow[rowstemp.Length];
}
if (lines.Length < rowstemp.Length)
Array.Copy(rowstemp, rows, lines.Length);
else
Array.Copy(rowstemp,rows,rowstemp.Length);

// delete the existing childrows, we have to create all of them
afterwards
for (int i = 0; i < tbItems.Rows.Count; ++i)
{
DataRow item = tbItems.Rows[i];
if (cb_selector.SelectedIndex ==
(int)item["selector_Id"])
{
item.Delete();
i = 0;
}
}

// create the entries
for (int i = 0; i < lines.Length; ++i)
{

DataRow childrow = rows[i];
childrow["item_Text"] = lines[i];
tbItems.Rows.Add(childrow);
}
With this code the output looks something like this:

<selections>
<selector name="foo" />
</selections>
<item>New Item</item>

But I want to have the new item under "foo".

TIA,
Patrick

May 4 '07 #1
  • viewed: 1063
Share:

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by davis | last post: by
1 post views Thread by Fleckman | last post: by
2 posts views Thread by Joe | last post: by
2 posts views Thread by randy1200 | last post: by
reply views Thread by sparty1022 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.