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.
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
>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.
Amy L. - 07 Sep 2004 20:10 GMT
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.
> Assuming you are using the XmlSerializer, your string array must be a
> public property with getter and setter methods. Beyond that I do not
[quoted text clipped - 45 lines]
> >Thanks
> >Amy.
Dino Chiesa [Microsoft] - 07 Sep 2004 21:35 GMT
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++);
}
}
}
> Excellent example, but it brings up one question. Is thier anyway to
> put some type of identification of which index it was pulled from?
[quoted text clipped - 67 lines]
> > >Thanks
> > >Amy.