473,379 Members | 1,386 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Nested XML and DataSets

Hello,

I have the following XML file:

<events>
<event name="event1">
<sessions>
<session date="23 Feb 2004" time="10:00 - 15:00" location="Toronto" />
</sessions>
</event>
<event name="event2">
<sessions>
<session date="25 Feb 2004" time="10:00 - 15:00" location="Toronto" />
<session date="26 Feb 2004" time="10:00 - 15:00" location="Toronto" />
</sessions>
</event>
</events>

I would like to read this file into any kind of storage within memory
(perhaps DataSets) keeping the relationship between "events" and "sessions":
Events ---one--------many---> Sessions

Then I need to modify, remove, and add new records to both "events" and
"sessions".

How to accomplish this? I need to incorporate the solution on ASP.NET web
forms.

Thanks for any hints,
Leszek Taratuta
Nov 12 '05 #1
3 2810
Hi Leszek,
Thanks for posting in the community!
From your description, you have a custom xml document used to store some
configure infos as below:
<events .. >
<event ..>
<sessions>
<session ... ....>
..
</sessions>
</event>
.....
......
</events>
And you'd like to use some certain class object to mantain them in memory
so as to manipulate them more Object-Orientedly ,yes?
If there is anything I misunderstood, please feel free to let me know.

Based on my understanding, the Xml Serialization of the dotnet framework is
exactly the solution you need. The Dotnet framework provide a group of
XmlSerilization Attributes for you to control your own custom classes so as
to map such class to certain format Xml Document. Then, we can use
XmlSerilizer Component to serialize the classes's object instances into Xml
files or deserialize xml document into class instances in memory. How do
you think of this? The most components of the dotnet Xml Serialiation
function are under the System.Xml and System.Xml.Serialization namespace.
Here are some detailed reference and tech artiles on the Xml Serialization
in dotnet:

#XML Serialization in the .NET Framework
http://msdn.microsoft.com/library/en...03.asp?frame=t
rue

#System.Xml.Serialization Hierarchy
http://msdn.microsoft.com/library/en...xmlserializati
onhierarchy.asp?frame=true

#Examples of XML Serialization
http://msdn.microsoft.com/library/en...ampleofxmlseri
alizationwithxmlserializer.asp?frame=true

#Controlling XML Serialization Using Attributes
http://msdn.microsoft.com/library/en...rollingseriali
zationbyxmlserializerwithattributes.asp?frame=true

As for your situation, we could define a group of custom classes to mapping
to the elements in your xml document, for example, here is a group of
sample classes I made:
-------------------------------------------class
file(events.cs)---------------------------------
using System.Xml.Serialization;
[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=false)]
public class Events {

[System.Xml.Serialization.XmlElementAttribute("even t",
Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public Event[] Items;
}

public class Event {
[System.Xml.Serialization.XmlArrayAttribute(Form=Sy stem.Xml.Schema.XmlSchema
Form.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("se ssion",
typeof(Session), Form=System.Xml.Schema.XmlSchemaForm.Unqualified,
IsNullable=false)]
public Session[] sessions;

[System.Xml.Serialization.XmlAttributeAttribute()]
public string name;
}

public class Session {

[System.Xml.Serialization.XmlAttributeAttribute()]
public string date;

[System.Xml.Serialization.XmlAttributeAttribute()]
public string time;

[System.Xml.Serialization.XmlAttributeAttribute()]
public string location;
}

--------------------------------------
Then, we can use the below code to create a certain events class instance
and serialize it into a xml file:
----------------------------------------
private void button2_Click(object sender, System.EventArgs e)
{
XmlSerializer serializer = new XmlSerializer(typeof(Events));

Events events = new Events();

Event[] arrEvent = new Event[3];
for(int i=0;i<3;i++)
{
Event evt = new Event();

Session[] arrSession = new Session[3];
for(int j=0;j<3;j++)
{
Session sn = new Session();
sn.location = "location_" + i.ToString() +"_" + j.ToString();
sn.date = "date_" + i.ToString() +"_" + j.ToString();
sn.time = "time_" + i.ToString() +"_" + j.ToString();
arrSession[j] = sn;
}
evt.sessions = arrSession;
arrEvent[i] = evt;
}

events.Items = arrEvent;

TextWriter writer = new StreamWriter("output.xml");
serializer.Serialize(writer,events);
writer.Close();
}
------------------------------------------------------

It is as simple as above to deserialize a certain format document into a
certain events instance(in memory).

Please try out the above suggestions and the code. If you feel any thing
unclear or need any further help, please feel free to let me know.
Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 12 '05 #2
Hi Leszek,
Have you had a chance to try out my suggestions or have you got any further
ideas on this issue? If you have any questions, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 12 '05 #3
Thanks a lot!
I am still working on it.

Leszek

"Steven Cheng[MSFT]" <v-******@online.microsoft.com> wrote in message
news:DP**************@cpmsftngxa07.phx.gbl...
Hi Leszek,
Have you had a chance to try out my suggestions or have you got any further ideas on this issue? If you have any questions, please feel free to post
here.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Yigit Ozgul | last post by:
Hi, I have a database where i keep 4 tables. the modules table: mdlID mdlName mdlstatus the submodules table:
3
by: Duncan Welch | last post by:
I have a strongly typed dataset that returns two tables - "items" and "itemdetails". In the strongly-typed dataset designer, I've created a link (relationship) between the two tables based on a...
4
by: Alpha | last post by:
I have a small Window application and through out the different forms I create a different dataset. At the begining I used the Tools to drag and drop the SqlDataAdapter, connection and dataset...
9
by: GaryDean | last post by:
We have been noticing that questions on vs.2005/2.0 don't appear to get much in answers so I'm reposting some questions posted by some of the programmers here in our organization that never got...
16
by: Luqman | last post by:
Is it recommended to use datasets in ASP.Net 2.0 / VS.Net 2005 ? Best Regards, Luqman
5
by: BMeyer | last post by:
I have been losing my mind trying to parse an XML document (with nested child elements, not all of which appear in each parent node) into a DataGrid object. What I want to do is "flatten" the XML...
1
by: Chris | last post by:
I am creating a nested gridview as per the tutorial here (http://msdn2.microsoft.com/en-us/library/aa992038(vs.80).aspx). My gridviews work fine. I have a master gridview containing the customerid....
0
by: S.Tedeschi | last post by:
Hi all; as posted some days ago, I'm converting an on-line app; I used to heavily rely on strongly-typed DataSets directly dropped onto pages, and so viewed by code(-behind) as well. In the next...
12
by: BillE | last post by:
I'm trying to decide if it is better to use typed datasets or business objects, so I would appreciate any thoughts from someone with more experience. When I use a business object to populate a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.