469,275 Members | 1,621 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,275 developers. It's quick & easy.

Serializing a class with a string[] array

I have a class that contains a string array. However, I can't get
this object to serialize in the xml output. Is there a trick to get a
string[] to serialize?

Thanks
Amy.
Nov 12 '05 #1
3 17763
Assuming you are using the XmlSerializer, your string array must be a
public property with getter and setter methods. Beyond that I do not
know of any tricks. A string array should serialize fine.

Here is a sample:

using System;
using System.Xml.Serialization;

namespace Sample
{
public class AppStart
{
public static void Main( string[] args )
{
MyClass myClass = new MyClass();
myClass.MyStrings = new string[] {"kilroy", "was", "here"};

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Serialize( Console.Out, myClass );
}
}

public class MyClass
{
private string[] _myStrings;

public string[] MyStrings
{
get{ return _myStrings; }
set{ _myStrings = value; }
}
}
}

HTH

-KIRBY
--
Kirby Turner, MCSD, MCAD
www.whitepeaksoftware.com

On 6 Sep 2004 19:27:52 -0700, am**@paxemail.com (Amy L.) wrote:
I have a class that contains a string array. However, I can't get
this object to serialize in the xml output. Is there a trick to get a
string[] to serialize?

Thanks
Amy.


Nov 12 '05 #2
Excellent example, but it brings up one question. Is thier anyway to
put some type of identification of which index it was pulled from?
Right now the XML looks like this.

<Test>
<string>Dog</string>
<string>Cat</string>
<string>Bird</string>
</Test>

But, I would rather have it do something like this, because in my app
knowing the placement of the index would be required. But this could
be a moot point if the object is always serialized from 0..N?
<Test>
<string index="1">Dog</string>
<string index="1">Cat</string>
<string index="1">Bird</string>
</Test>

Any thoughts
Amy.

Kirby Turner <ki***@whitepeaksoftware.com> wrote in message news:<3g********************************@4ax.com>. ..
Assuming you are using the XmlSerializer, your string array must be a
public property with getter and setter methods. Beyond that I do not
know of any tricks. A string array should serialize fine.

Here is a sample:

using System;
using System.Xml.Serialization;

namespace Sample
{
public class AppStart
{
public static void Main( string[] args )
{
MyClass myClass = new MyClass();
myClass.MyStrings = new string[] {"kilroy", "was", "here"};

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Serialize( Console.Out, myClass );
}
}

public class MyClass
{
private string[] _myStrings;

public string[] MyStrings
{
get{ return _myStrings; }
set{ _myStrings = value; }
}
}
}

HTH

-KIRBY
--
Kirby Turner, MCSD, MCAD
www.whitepeaksoftware.com

On 6 Sep 2004 19:27:52 -0700, am**@paxemail.com (Amy L.) wrote:
I have a class that contains a string array. However, I can't get
this object to serialize in the xml output. Is there a trick to get a
string[] to serialize?

Thanks
Amy.

Nov 12 '05 #3
if the order is important then you need an additional property to hold that
information. I don't believe it is guaranteed in Xml Serialization.
Example:

using System;
using System.Xml.Serialization;

namespace Sample
{
public class AppStart
{
public static void Main( string[] args )
{
MyClass myClass = new MyClass();
myClass.Fill(new string[] {"kilroy", "was", "here"});

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Serialize( Console.Out, myClass );
}
}

public class Tuple {
[XmlText]
public string value;
[XmlAttribute]
public int index;
public Tuple() {}
public Tuple(string _s, int _i) {
index= _i;
value= _s;
}
}

public class MyClass
{
private Tuple[] _myThings;
private int _currentIndex=0;

[XmlElement("Thing", typeof(Tuple))]
public Tuple[] MyThings
{
get{ return _myThings; }
set{ _myThings = value; }
}

public void Fill(string[] _s) {
_myThings= new Tuple[_s.Length];
for (int i=0; i< _s.Length; i++)
_myThings[i]= new Tuple(_s[i],_currentIndex++);

}
}
}

"Amy L." <am**@paxemail.com> wrote in message
news:c7**************************@posting.google.c om...
Excellent example, but it brings up one question. Is thier anyway to
put some type of identification of which index it was pulled from?
Right now the XML looks like this.

<Test>
<string>Dog</string>
<string>Cat</string>
<string>Bird</string>
</Test>

But, I would rather have it do something like this, because in my app
knowing the placement of the index would be required. But this could
be a moot point if the object is always serialized from 0..N?
<Test>
<string index="1">Dog</string>
<string index="1">Cat</string>
<string index="1">Bird</string>
</Test>

Any thoughts
Amy.

Kirby Turner <ki***@whitepeaksoftware.com> wrote in message

news:<3g********************************@4ax.com>. ..
Assuming you are using the XmlSerializer, your string array must be a
public property with getter and setter methods. Beyond that I do not
know of any tricks. A string array should serialize fine.

Here is a sample:

using System;
using System.Xml.Serialization;

namespace Sample
{
public class AppStart
{
public static void Main( string[] args )
{
MyClass myClass = new MyClass();
myClass.MyStrings = new string[] {"kilroy", "was", "here"};

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Serialize( Console.Out, myClass );
}
}

public class MyClass
{
private string[] _myStrings;

public string[] MyStrings
{
get{ return _myStrings; }
set{ _myStrings = value; }
}
}
}

HTH

-KIRBY
--
Kirby Turner, MCSD, MCAD
www.whitepeaksoftware.com

On 6 Sep 2004 19:27:52 -0700, am**@paxemail.com (Amy L.) wrote:
I have a class that contains a string array. However, I can't get
this object to serialize in the xml output. Is there a trick to get a
string[] to serialize?

Thanks
Amy.

Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by exitus | last post: by
reply views Thread by ron | last post: by
10 posts views Thread by mattias.k.nyberg | last post: by
2 posts views Thread by =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post: by
2 posts views Thread by LinLMa | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.