DataSource WriteXML printing child rows for associate parent - c#

In my database, I have a parent and child tables.
For example, parent table contains last name and address, and child table contains last and first name.
There is a foreign key, so that a row in child table must have corresponding last-name in parent table.
When I read those two tables using 2 DataAdapter's, and add those DataTable's to DataSet, I'd like to print XML, that looks like this:
<parent_table>
<last_name>Smith</last_name>
<address>111 Hi Street, Bye city</address>
<child_table>
<last_name>Smith</last_name>
<first_name>Ann</first_name>
</child_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Bob</first_name>
</child_table>
</parent_table>
However, at present I'm getting two tables printed separately:
<parent_table>
<last_name>Smith</last_name>
<address>111 Hi Street, Bye city</address>
</parent_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Ann</first_name>
</child_table>
<child_table>
<last_name>Smith</last_name>
<first_name>Bob</first_name>
</child_table>
Is there a way to achieve (hopefully using DataSet.WriteXML()) my desired output?
I tried adding ForeignKeyConstraint, and tried adding DataRelation, but neither changed the output.
Disclaimer: the above was hand-written, so please excuse if there's an error in XML. The actual tables contain better foreign key than 'last-name'.

From Writing DataSet Contents as XML Data (ADO.NET):
When writing an XML representation of a DataSet that contains
DataRelation objects, you will most likely want the resulting XML to
have the child rows of each relation nested within their related
parent elements. To accomplish this, set the Nested property of the
DataRelation to true when you add the DataRelation to the DataSet. For
more information, see Nesting DataRelations (ADO.NET).

Related

DocFx.console v2.59.4 making a nested table span all columns

When I generate my docfx documentation, my Fields have the proper <name> and <description> columns. However, a nested table is created inside the description column that can be fairly large and it leaves the <name> column with a lot of empty space (see Figure 1). I was wondering if it is possible to make it span all the columns or place it under the parent table?
What it looks like:
What I want it to look like:
I inspected the developer tools and the tables have the exact same class so I could not think of a way to solve this problem without manually going in and changing them.

Loading a hierarchy with one hit to a database using Linq

We have a table structure like an order table with an order lines table and we define a hierarchy of items in the table according to the user requirements by storing a string made up of a comma separated list of record id’s at each level. This allows the user to “re-order” their lines as needed.
e.g.
A Hierarchy string on the order could be something like “3,2,4” meaning that its top level children are lines 3,2,4 (in that order).
The first order line might have its own hierarchy string such as “5,6” indicating that it has nested children that should appear below it.
We want to return this data structure as a flat list of objects in order with a marker of how “deep” it is in the hierarchy. For example if the data looks like this:
Header:
Lines:
We want a flat list like this (in this order):
{Line1Object}, 1
{Line3Object}, 2
{Line2Object}, 2
{Line4Object}, 2
{Line5Object}, 3
{Line6Object}, 3
Can this be done in one hit with some kind of joining process in Entity Framework instead of looping through our records and building this up as we go or are we being too ambitious?

How does DataSet.ReadXml(TextReader) handle nested XML elements?

I'm a PHP programmer, and I'm trying to understand some code which I think is ASP.NET. This is also my first foray into XML. I don't have access to a Windows box to test on.
I need to produce XML output that third-party code can use. The third party wants to use our data instead of the data source they are currently using. I don't want to replicate the current XML structure exactly because it doesn't map well to our data.
The structure of the current XML is very flat. There are only a few nested elements and the third party doesn't make use of any of them. The third party does have a sub-contracted programmer, but he is very busy. Also, I want to understand, for myself, how this works.
This is an excerpt from a plugin for a custom CMS:
Dim obj_set As New Data.DataSet()
Using obj_reader As New System.Xml.XmlTextReader("http://www.example.com/xml_output.php")
obj_set.ReadXml(obj_reader)
End Using
Dim obj_view As Data.DataView = obj_set.Tables("profile").DefaultView
obj_view.Sort = "cname"
Dim obj_data As Data.DataTable = obj_view.ToTable()
So from what I have gathered so far, this code
reads the XML file into a DataSet
sorts the profile table by cname
creates a new DataTable from the sorted view
There is other code that stores the new table to, and retrieves it from, cache. Then there is code that loops through the table rows and maps the column names to template variables.
Sample excerpt of current XML structure:
<profiles>
<profile>
<cname>ABC Corporation</cname>
<fname>John</fname>
<lname>Smith</lname>
<sector>Widgets</sector>
<subsectors>
<subsector>Basic Widgets</subsector>
<subsector>Fancy Widgets</subsector>
</subsectors>
</profile>
</profiles>
So what happens to the subsectors data? Does the reader create a separate table for it? If so, how are the tables related?
Our data includes multiple contacts per company. I could just create multiple elements at the top level fname1, fname2, fname3 to keep the flat structure. But I was thinking a nested structure makes sense for this kind of data. The problem is that I don't understand if such a structural change is compatible with the plugin code.
What kinds of changes would need to be made to the plugin code to make use of nested elements?
I was stumped on this myself, and I don't know if you still are, but for reference to others here's what I found.
You are right in assuming that the reader creates a separate table for it. Being that a DataSet can hold multiple tables, each "level" of elements gets its own table. However, any nested elements that have nested elements of their own will get their own table. Essentially, it keeps creating tables until it reaches the bottom of the xml tree. If an element has no children, it gets added as a cell in the data table.
In your case,
dataSet.Tables[0] will hold the top level nodes (all the <.profiles>). But since the nested element <.profile> has elements of its own, Tables[0] will likely only have one row. The next level deeper, dataSet.Tables[1] will hold all <-profile> nodes. Although since <.subsectors> has sub-element <.subsector>, it will not be in Tables[1], but rather in Tables[2] which goes yet level deeper.
I know it has been a while since this was asked, but hopefully this will be helpful.

Add a field to a child DataSet C# using Rows.add & adapter.update

I added some data to a parent DataSet and need to add something to a child table. Everything is ok except I need a number of a row form the parent table (the one I just added). So, I did adapter.update for the parent table and when I try to get
ds.tables["Parent"].rows[ds.tables["parent"].rows.count-1][0]
it gives me null. Is it possible to get the last number from the parent table?
Ok. I solved my problem by disposing BbConnection and creating it again. (I don't want to deal with it write now but I disposed and recreated everything including DataSet and Adapters). So looks like
adapter.update(dataset,table_name);
will update a record you just added either after adding another record or after restarting the program. So in my case I needed to add a record to a parent table and several depended records to a child table inferring to the last record from the parent table. Another solution of course is to avoid relations but I wanted to have a program with them.
If anyone has better solution I will be happy to learn it.

Populating related datasets from a table adapter

I'm using table adapters and datasets in .NET (version 2.0).
I have two tables like this:
Table1
------
...
TypeId
Table2
------
Id
Name
where Table1.TypeId is linked to Table2.Id.
I've strongly generated these types by using the wizard so now I can do things like this:
Table1Adapter adapter = new Table1Adapter();
Table1DataSet data = adapter.GetData();
foreach(Table1Row row in data) { ... }
// Can now iterate through and display the data
This all works fine. But now I also want the data from Table2. I have noticed that in row there is generated field Table2Row which seems ideal but it is set to null. How do I populate this dataset properly?
Each DataTable in Typed-Dataset has its own TableAdapter. So you'll have to the same step for each DataTable in the dataset.
There does not exists any api that lets you do this auto-fill of the entire typed-dataset or no such code is generated within typed-dataset that supports this. It is also difficult to do this because TableAdapters do not have a common base-class that can let you do this.
If you really need to do this, you'll have to maintain a collection of DataTable type-names and TableAdapter type-names and iterate over the collection to perform the dataset fill.
So I recommend to fill dataset for each table in 'hard-code' manner.
Actually there is a Microsoft Data Provider called `MSDataShape', which provides this functionality. It was flagged for depreciation last when I checked and I don't know what the current status and future plans are, or what replaces it.
Here's an example of a single "SQL command", that will return, I believe, a DataSet with two nicely related DataTable-s:
SHAPE {select * from customers}
APPEND ({select * from orders} AS rsOrders
RELATE customerid TO customerid)
Potential replacement to look at is well-crafted FOR XML query, but I have not played with that.
EDIT:
I think this SO answer does exactly that using XML.

Categories