Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Serialize a class

Posted on 2004-08-26
6
Medium Priority
?
1,713 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

610 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