Lost in the XML jungle

O

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
 
C

Chris Dunaway

Ole said:
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.
 
O

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 said:
Ole said:
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.
 
C

Chris Dunaway

Ole said:
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/
 
O

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 said:
Ole said:
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top