?
Solved

Serialize a class

Posted on 2004-08-26
6
Medium Priority
?
1,712 Views
Last Modified: 2008-03-10
I'm trying to serialize a custom class that needs to use multiple elements of the same name.  I've tried using xmlarray, but it wraps them in another element.  


I want my xml to look like this.
<xmlroot>
     <element>some text</element>
     <element>some more text</element>
</xmlroot>

My code:
[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
      [XmlArray("element")]
      public ArrayList MyProp1 = new ArrayList();
       
      public xmlroot()
      {
           MyProp1.Add("some text");
           MyProp1.Add("some more text");
           

      }
}

I've also tried this, but it throws reflection errors when trying to serialize:

[Serializable(), XmlRoot("xmlroot")]
public class xmlroot
{
      [XmlElement("element")]
      public string MyProp1;
     
      [XmlElement("element")]
      public string MyProp2;

      public xmlroot()
      {
           MyProp1 = "some text";
           MyProp2 = "some more text";
      }
}

After I serialize, it looks like this:
<xmlroot>
     <element>
           <anyType xsi:type="string">some text</anyType>
           <anyType xsi:type="string">some more text</anyType>
     </element>
</xmlroot>
0
Comment
Question by:darthg8r
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:johanjohansson
ID: 11910547
What if you make the xmlroot class into a list:

[Serializable(), XmlRoot("xmlroot")]
public class xmlroot : CollectionBase
{
   [XmlElement("element")]
   public string this[int index]
   {
      get { ... }
      set { ... }
   }

   public int Add( string element ) { ... }
   public void Remove( int index ) { ... }
}
0
 
LVL 1

Expert Comment

by:johanjohansson
ID: 11910570
You can also feed your XML into the Xsd.exe tool to get the object model (classes) defined for you.
0
 
LVL 2

Accepted Solution

by:
sonicblis earned 750 total points
ID: 11912481
Here is the serializable class:

[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public class xmlroot {
   
    [System.Xml.Serialization.XmlElementAttribute("element", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=true)]
    public xmlrootElement[] Items;
}

public class xmlrootElement {
   
    [System.Xml.Serialization.XmlTextAttribute()]
    public string Value;
}

Steps to producing this code:

1. Create the xml file you want to end up with in notepad (or an editor of your choice)
2. Use the xsd.exe tool (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cptools/html/cpconXMLSchemaDefinitionToolXsdexe.asp) to generate an xsd [xsd -c -l:c# yourxml.xml]
3. Use the xsd.exe tool to generate the classes that will be serialized appropriately [xsd -c -l:c# yourschema.xsd]
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 7

Expert Comment

by:NipNFriar_Tuck
ID: 11913615
What has already been stated is true... As to why you ended up with the xml like you did is that an Array is translated into an XML Sequence and since you did not use an attribute to specify the type of data in the array the translation asumed anyType.  Hope this helps...
0
 
LVL 10

Expert Comment

by:123654789987
ID: 11919841
Try this

[Serializable(), XmlRoot("xmlroot")]
public class XmlRoot:  CollectionBase
      {
            
      public int Add(Element element)
            {
                  return this.InnerList.Add(element);
            }


            [XmlArrayItem("Element")]
            public  Element this[int index]
            {
                  get
                  {
                        return this.InnerList[index] as Element ;
                  }
                  set
                  {
                        this.InnerList[index]=value;
                  }
            }

      }
0
 

Expert Comment

by:_dataking_
ID: 11957160
An arraylist member is properly serialized as shown below, assuming the objects in the arraylist is of type "Item":

// This attribute enables the ArrayList to be serialized:
[System.Xml.Serialization.XmlArray("Items")]
// Explicitly tell the serializer to expect the Item class
// so it can be properly written to XML from the collection:
[System.Xml.Serialization.XmlArrayItem("item",typeof(Item))]
public ArrayList myArrayList;

HTH
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question