473,657 Members | 2,554 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 18806
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.Seri alization;

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

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Seri alize( 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.c om (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***@whitepea ksoftware.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.Seri alization;

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

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Seri alize( 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.c om (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.Seri alization;

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

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Seri alize( 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("Thi ng", 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.goo gle.com...
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***@whitepea ksoftware.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.Seri alization;

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

XmlSerializer serializer = new XmlSerializer( typeof( MyClass )
);
serializer.Seri alize( 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.c om (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
1742
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 (sTest) is initially. As stated in the code below, when the class is instantiated, the string array is initialized to null. Is it possible to have the user "set" a value in the array and the object would resize the array automatically? For...
1
2871
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 string a;
0
1371
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, each of the elements is a single-dimensional array of integers in the second array. Why dose the XML read as such for the first element "<ArrayOfInt>" and "<int>" for the second element.
5
10033
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
6046
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 error that reads: You must implement the Add(System.Object) method on LineItems because it inherits from IEnumerable. I've implemented GetEnumerator on the LineItems class. I also have two Add methods.
10
6252
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 thing. class Test { string words; public Test() { words = { "", "", "" }; // This doesn't work }
1
4858
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 down for a way to do this but i havn't come arcoss anything that can help with this problem.
2
2876
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. Anyway, I have a structure that has several members including an array of strings. I assign the elements of the string array from the .Text property of several TextBoxes. I then serialize the structure and write it to a file. However, when I...
2
2222
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 address of the array; 2. When dereferencing it twice (operator **), why the result is always
0
8305
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6163
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.