XML building off of a schema
XML building off of a schema
XML building off of a schema
XML building off of a schema
XML building off of a schema
XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema
XML building off of a schema XML building off of a schema
XML building off of a schema
Go Back  Xtreme Visual Basic Talk > > > XML building off of a schema


Reply
 
Thread Tools Display Modes
  #1  
Old 08-31-2006, 10:56 AM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default XML building off of a schema


Ok, I am fairly new to .net and do not know how to utilize all of the new features. I am at a point where I have to build a xml file based upon a schema. In the past I would just create nested loops to build the xml. With .Net there are new tools wich I will probably use although I am not sure what is best or if my expectations of them are reasonable so any guidence would be useful.

xml writer...seems like the same way as I am used to but with different tags to help see what is going on.

xml datadocument..not too sure how this works

dataset...load in the schema creates multiple tables but I am unsure how to insert the related data.

These are the things that I have been looking at. Suggestions about what path I should look into or a differnt way I should learn would be useful. I'm sure there are plenty of ways to do this I was just wondering what way was the best(fastest,easiest, most versitle).

Thank you.
Reply With Quote
  #2  
Old 09-01-2006, 07:05 AM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

The schema support in .NET is mostly for creation of schemas and validation of existing xml. You could load a schema via XmlSchema.Read, enumerate the schema (through the Items collection) and use XmlTextWriter or XmlDocument to write the xml based on the schema, but the DataSet wraps this functionality pretty well.

The primary difference between XmlTextWriter and XmlDocument/XmlDataDocument is that the writer does not have in-memory structure that can be manipulated. The XmlDataDocument is just a XmlDocument that also supports access to its structure via a related DataSet.

The easiest way to get a structure that allows you to build a xml file based on the schema would be the DataSet - check out the class references for DataSet, DataTable and DataRow on MSDN. You can also generate strongly-typed DataSet classes based on a xml schema.

One other way to get a structure would be to create a custom tool/build provider that creates an object model based on a schema, but the only real benefit to this approach that I see is a strongly-typed object model that is not as heavy as a strongly-typed DataSet.
Reply With Quote
  #3  
Old 09-01-2006, 08:19 AM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

First off thank you for the reply. That helps narrow down what I should be looking at. I have a little bit of knowledge with the dataset, but I don't know too much about it. I think I know the basics but I am not sure how to accomplish the xml thing with it due to not knowing how to add items to the tables based upon relational data.

ex. Lets say I have a group of items that have hourly values with subsets of differnet states. The dataset will break the xml into three tables(Unit, HR, SUBDATA) or so.

This where I run into problems you have all the unit and their constant data in one table. You have the hourly data in another table. You also have the sub hourly data in another table. You need to use parent relations to see what hr the subdata is for and again to see the unit. This is fine, but I don't understand how to insert data into the structure that would designate the relationships necissary to retrieve the data and/or ultimately create the xml.
Reply With Quote
  #4  
Old 09-01-2006, 09:06 AM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Ah, I see. I know you can create DataRelation objects to create foreign-key relationships between the DataTable object; I've never actually used them but it doesn't seem to difficult - just create a new DataRelation object and add it to the DataSet.Relations collection.
Reply With Quote
  #5  
Old 09-01-2006, 09:41 AM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

Well, I am not too familiar with foreign keys, but wouldn't they be created with the schema? From what I can tell, there are relations present, I just don't know how to write to the dataset in a way that would let it know the value and the corresponding related values linked through the relations. Ideally I would like to include all the data and have it sort it into the tables.
Reply With Quote
  #6  
Old 09-01-2006, 11:40 AM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

You're right - the relations are created from the relationships in the schema.

When using data relations, you just add rows to the tables (using the DataTable.NewRow and DataTable.Rows.Add methods) while ensuring the data in the row(s) doesn't violate the constaints; otherwise an exception is thrown:
Code:
Dim ds As New DataSet Dim t1 As New DataTable t1.Columns.Add("PK") Dim t2 As New DataTable t2.Columns.Add("PK") t2.Columns.Add("FK") ds.Tables.Add(t1) ds.Tables.Add(t2) Dim fk As New DataRelation("FK", t1.Columns("PK"), t2.Columns("FK")) ds.Relations.Add(fk) Dim parentRow As DataRow = t1.NewRow() parentRow("PK") = "P" Dim childRow As DataRow = t2.NewRow() childRow("PK") = "C" childRow("FK") = "P" 'Reverse the order of these lines to cause a data relation exception. t1.Rows.Add(parentRow) t2.Rows.Add(childRow)
Reply With Quote
  #7  
Old 09-06-2006, 11:59 AM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

With this though, you still have to work your way the entire way up and down the relations though, right, or at least go in a specific order as your child row appears to only be related the parent in a way that it is the most recent. If you already have the majority of the data, but need to alow additions to the data, i still only see the options of placing the data and working your way up/down the later go relate it as needed. I know I can/have done this but wouldn't it be nice to create a datarow (or something) and use the dataset.load command to place the data in the dataset where neccisary as oppose to specifing the parent/children all the way up and down the structure? This is kind of what I was hoping for. I haven't had anyone tell me it can't be done nor found it can't, but no one seems to know how to either. I could use any help as it sometimes leads to better ideas.
Reply With Quote
  #8  
Old 09-06-2006, 12:40 PM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

So you're looking for a way to add children without necessarily having a parent? You can do this by disabling the data relation(s) (set the DataSet.EnforceConstraints property to False), but of course your data would then violate the key(s)/relation(s). In order to ensure the data is valid you would still need to provide the data (keys) that correctly link parent and child(ren) together - you can auto-generate keys for the parent rows but there isn't any way to automagically link a child to a specific parent.

Or am I not understanding the question?
Reply With Quote
  #9  
Old 09-08-2006, 04:25 PM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

I am not looking for a way to add children without a parent. I am looking for an easy way to find the parent for my children and work it all the way back up. I will have a xml file with a good amount of the data that I need. I need to add little bits here and there. In this case, the parents will already be there and the parent to child relations will be built. The information that I will be adding will have the topmost parent information (Unit) and the bottommost child information. Depending on what schema i need for each situation I need to put the info in the correct place of the schema. If I need to know all the reations for each schema thats fine(but would be nice if I didn't), but what I am really looking for is a easy way to relate the bottommost data, all the way back up to the topmost when the topmost is already present. How do I go about finding relating the values back up the schema. Lets say in your example parentrow was already living in t1 and not neccisarrily the last one, you knew childrow was realated to parentrow, how would you relate childrow to parentrow.
Reply With Quote
  #10  
Old 09-08-2006, 04:48 PM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

The relationships between parent and child are defined by the fields included in the relation/key/constraint.

In my example the parent field "PK" is related to the child field "FK" in the data relation I created, so for a child row to be related to a parent row it must have a value in it's "FK" field that matches a parent row with the same value in it's "PK" field. If you changed the code sample so that the child's "FK" field was given a value of "A" it would violate the data relation (no parent row has a value of "A" in the "PK" field).
Reply With Quote
  #11  
Old 09-08-2006, 07:15 PM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

Ok, well thats fine, but when I have more than two tables however, I will have some intermediate steps. In addition, I don't think that when you load a sql it structures it the same. It gives the tables fields that represent the names in a numerical form. This is because the intermediate fields could contain things like "Data" but the parents aren't neccisarily the same. So I'm looking for an easier way to input the data than to search all the indexs in these fields to find my way up/down the relations because I many more than two tables to work through.
Reply With Quote
  #12  
Old 09-14-2006, 07:56 PM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Child entities are related to parent entities via some common value (or set of values) - I believe this holds true for all relational systems. If the entities don't have the required value (or set) in common, then there's no relationship. The only way to determine if a value is valid is to search by whatever means you have available.

Do you happen to have a sample XML file and/or schema? It might help to make things a little more clear.
Reply With Quote
  #13  
Old 09-16-2006, 01:31 PM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

Well this is being done as an eventual project (not top priority) for work so I am not realy able to use the info, but the situation is similar to
<Body>
-<Day loc:>
--<Info1 hour=#>
---vals
--<Info2>
---<subcategories>
--<Info3>
---<Info hour=3 val=>
.....
so the problem is I now have values to put into here but they come from a database so its more like [Day][Hour][Info1][Info3].... so now i know all the information, but the relation from day down to values are not the same and not in the same place so I can't even create a iterative loop. This is why I was hoping for an easy way to say something like load datarow, where it would see the day hour and info and place it where neccissary for each case, or at least a simmilar way.
Reply With Quote
  #14  
Old 09-16-2006, 08:36 PM
shaul_ahuvaXML building off of a schema shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992
Default

Okay, I see the issue now. Unfortunately, I can't think of anything directly supported by the framework that would do what you want to accomplish.

The first (although probably far from the best) solution that comes to mind would be to serialize the data from the database to a xml stream (via DataTable.WriteXml), transform that xml into the proper structure with a stylesheet and read the transformed xml with the DataSet.ReadXml method.

The serialized DataTable would be something like:
HTML Code:
<DocumentElement>
     <tableName>     <!-- Repeats for every DataRow in the table -->
          <Day></Day>
          <Hour></Hour>
          <Info1></Info1>
          <Info2></Info2>
     </tableName>
</DocumentElement>
You might be able to save yourself a step or two if the DBMS you're using supports returning the resultset as xml, however my experience with xml support in database systems is limited at best.

If anything else comes to mind, I'll let you know...
Reply With Quote
  #15  
Old 09-19-2006, 02:41 PM
JJKazJr JJKazJr is offline
Regular
 
Join Date: Jun 2006
Location: Milwaukee, WI
Posts: 68
Default

OK, thank you, especially for sticking with me through this. I at least know that I am not overlooking something easy. I really appreate your help and determination.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
XML building off of a schema
XML building off of a schema
XML building off of a schema XML building off of a schema
XML building off of a schema
XML building off of a schema
XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema XML building off of a schema
XML building off of a schema
XML building off of a schema
 
XML building off of a schema
XML building off of a schema
 
-->