473,513 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Lost in the XML jungle

Ole
Hi there,

I think I'm lost in the XML attribute jungle in my attempt to
serialize/deserializing data that must be persistant, and any help is highly
appriciated.

I need to save this class in an XML file and it must be unicode formatted:

class classToSave

{

public string _dataString1 = ""; //must be saved

public Arrayelement[] _array = new arrayElement[0]; // must be saved

private string _fileName; // name of the XML file

public classToSave(string fileName)

{

_fileName = fileName;

/*** loadXML here ***/

}

public string dataString1

{

get

{

return _dataString1;

}

set

{

_dataString1= value;

/*** SaveXML here ***/

}

}

public arrayelement[] array

{

get

{

return _array;

}

set

{

_array = value;

/*** SaveXML here ***/

}

}
And the arrayelement looks like this:
public struct arrayElement

{

public int element1;

public double element2;

}

Thanks

Ole
Jun 16 '06 #1
4 1470
Ole wrote:

I need to save this class in an XML file and it must be unicode formatted:

Here is one way to do it, but you might have to tweak it a little to
get exactly what you want, but perhaps it is a start:

First, add the Serializable attribute to the class:

using System.Xml.Serialization;

[Serializable] class classToSave

{

public string _dataString1 = ""; //must be saved

public Arrayelement[] _array = new arrayElement[0]; // must be saved

private string _fileName; // name of the XML file

public classToSave(string fileName)

{

_fileName = fileName;

/*** loadXML here ***/

}

If you want your properties to be saved as attributes rather than
elements, use the XmlAttribute attribute:

[XmlAttribute("DataString")] public string dataString1

{
get
{
return _dataString1;
}

set
{
_dataString1= value;
}
}


The code to save it is like this:

Using (StreamWriter sw = new StreamWriter("filename.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(classToSave));
xs.Serialize(sw, instancevar);
}

The code to deserialize is very similar:

Using (StreamReader sr = new StreamReader("filename.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(classToSave));
classToSave var = (classToSave) xs.Deserialize(sr);
}
Look at the other classes in the Xml.Serialization namespace. They can
help you to further tweak the output of the serializer.

Hope this helps.

Jun 16 '06 #2
Ole
Thanks Chris!

Will the array be saved the same way with the statement:
[XmlAttribute("Array")]? What is the difference between a XmlAttribute
attribute and an element?

Thanks
Ole
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11*********************@f6g2000cwb.googlegrou ps.com...
Ole wrote:

I need to save this class in an XML file and it must be unicode
formatted:


Here is one way to do it, but you might have to tweak it a little to
get exactly what you want, but perhaps it is a start:

First, add the Serializable attribute to the class:

using System.Xml.Serialization;

[Serializable]
class classToSave

{

public string _dataString1 = ""; //must be saved

public Arrayelement[] _array = new arrayElement[0]; // must be saved

private string _fileName; // name of the XML file

public classToSave(string fileName)

{

_fileName = fileName;

/*** loadXML here ***/

}


If you want your properties to be saved as attributes rather than
elements, use the XmlAttribute attribute:

[XmlAttribute("DataString")]
public string dataString1

{
get
{
return _dataString1;
}

set
{
_dataString1= value;
}
}


The code to save it is like this:

Using (StreamWriter sw = new StreamWriter("filename.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(classToSave));
xs.Serialize(sw, instancevar);
}

The code to deserialize is very similar:

Using (StreamReader sr = new StreamReader("filename.xml"))
{
XmlSerializer xs = new XmlSerializer(typeof(classToSave));
classToSave var = (classToSave) xs.Deserialize(sr);
}
Look at the other classes in the Xml.Serialization namespace. They can
help you to further tweak the output of the serializer.

Hope this helps.

Jun 16 '06 #3
Ole wrote:
Will the array be saved the same way with the statement:
[XmlAttribute("Array")]?
You can't really have an array be an *attribute* it needs to be an
element (See below).

Off the top of my head, I'm not 100% sure how an array will be
serialized. I suggest running the save code and examining the file it
produces.
What is the difference between a XmlAttribute attribute and an element?


When I wrote element I intended a node:

<RootElement>
<Element1 Attribute1="Value" Attribute2="value">
InnerText
</Element>
</RootElement>

Here is a useful link that has a large xml section:

http://www.w3schools.com/

Jun 16 '06 #4
Ole
Thanks Chris,

Your help and the link was very helpfull - I'll have to read a bit more
about the topic I think :-)

Ole
"Chris Dunaway" <du******@gmail.com> wrote in message
news:11**********************@c74g2000cwc.googlegr oups.com...
Ole wrote:
Will the array be saved the same way with the statement:
[XmlAttribute("Array")]?


You can't really have an array be an *attribute* it needs to be an
element (See below).

Off the top of my head, I'm not 100% sure how an array will be
serialized. I suggest running the save code and examining the file it
produces.
What is the difference between a XmlAttribute attribute and an element?


When I wrote element I intended a node:

<RootElement>
<Element1 Attribute1="Value" Attribute2="value">
InnerText
</Element>
</RootElement>

Here is a useful link that has a large xml section:

http://www.w3schools.com/

Jun 16 '06 #5

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

Similar topics

6
12943
by: Tony G. | last post by:
Hi there, I have an APS 3 application, running on a Windows 2003 Web edition server - it is a very busy website, and when users are click on certain links (membership info), a new window i...
3
2594
by: William | last post by:
Hi I have an ASP.NET application that connects to an Access database. Everything works fine except for the Session object. Data in the session object is lost after I've made a call to the...
9
3232
by: AndersBj | last post by:
Hi all, I have a web application that uses State Server for session handling. The web application randomly loses all session variables. The sessions are not always lost, sometimes I can use the...
3
3819
by: Tom | last post by:
I have a VB .NET application that has a text box with the following code to handle the leave event. Private Sub txtIDiscountRate_TextChanged(ByVal sender As System.Object, ByVal e As...
0
1695
by: Nick | last post by:
I have a situation wherein i have a text box, that has abc() being executed whenever the lost focus event is triggered. However, there is an exceptional case where, if a button click causes the...
0
1708
by: BerkshireGuy | last post by:
Has anyone come across a Lost and Found database to track a hotel's lost and found items? I would be curious to review a system of such. I've created one myself and have a question regarding...
7
2946
by: Erik | last post by:
I have an application that uses sessions variables a lot but one I publish the application on the prod server these variables are lost. The application is written i c# 2.0 and I've set the...
2
4230
by: maxkumar | last post by:
Hi, I am running a ASP.NET 1.1 site on Win Server 2003 with IIS 6.0. The website has been running for about 1.5 years now. In the past, we used to have random cases of session variables getting...
3
4199
by: coolsti | last post by:
I have experience with C++, Java, PHP and many many more "older" software languages. More recently I got introduced to C# and programmed some small applications with Visual Studio 2005 Express...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5086
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.