By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,527 Members | 2,155 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,527 IT Pros & Developers. It's quick & easy.

Nested XML and DataSets

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.