Solved

Serialize a class

Posted on 2004-08-26
6
1,695 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
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 250 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

895 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now