473,390 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,390 software developers and data experts.

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 18768
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: exitus | last post by:
Is there a way to dynamically resize an array along with maintaining its contents similar to VB's REDIM PRESERVE statement in C#? My problem is, the user won't be sure how big the string array...
1
by: Dave | last post by:
Hello, I'm trying to solve an XML serialization problem that appears to me to be be a bug with the XmlSerializer. Let's say I have a class that looks like this: public class y { public...
0
by: ron | last post by:
Hi, I am Serializing a Jagged Array and it works fine. The qusetion i have is way dose the XML formatting do the following. I have a single-dimensional array that has 21 elements of type int,...
5
by: Mark Ingram | last post by:
Hi, how can i return an array of strings from an unmanaged c++ dll into a c# application? cheers Mark
5
by: Craig Buchanan | last post by:
I have a class called LineItems. It implements IEnumerable. It uses an ArrayList to hold objects. When I try to serialize the object that contains LineItems (an object called Order), I get an...
10
by: mattias.k.nyberg | last post by:
So Im trying to learn to program with C#. And I have this question about why the string array won't work in the first class but it does in the second. To me it looks like they do the exact same...
1
by: dotnetnoob | last post by:
i have a arraylist that store String() array the string array hold value 1 2 4 i want to access the string array that is inside the arraylist one arraylist item at the time i search up and...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
2
by: LinLMa | last post by:
Hello everyone, I find strange result in the following program. 1. For string array, dereferencing it will result in the string itself, but for int array, dereferencing it will result in the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.