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

Serializing a class with a string[] array

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


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

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

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