Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2004

Tip: Looking for answers? Try searching our database.

Serializing a class with a string[] array

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Amy L. - 07 Sep 2004 03:27 GMT
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.
Kirby Turner - 07 Sep 2004 15:50 GMT
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.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.