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 / Languages / Visual J# / May 2004

Tip: Looking for answers? Try searching our database.

InvalidOperationException was unhandled

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Prasad Godde - 14 May 2004 07:16 GMT
I am getting unhandled InvalidOperationException at runtime for statemen
mySerializer.Serialize(writer, myGroup)
at line no. 6

Could anybody please helpout in excecuting this code

//<Snippet1
import System.*
import System.IO.*
import System.Xml.*
import System.Xml.Serialization.*

public class Grou

   public String groupName
   public GroupType groupType
} //Grou

public class GroupTyp

   // Use the SoapEnumAttribute to instruct the XmlSerialize
   // to generate Small and Large instead of A and B
   public int member

   public GroupType(
   
       member = 0
   } //GroupTyp

   public GroupType(int n
   
       member = n
   } //GroupTyp

   /** @attribute SoapEnum("Small"
    *
   public static int a = 0

   /** @attribute SoapEnum("Large"
    *
   public static int B = 1
} //GroupTyp

public class Ru

   public static void main(String[] args
   
       Run test = new Run()
       test.SerializeObject("SoapEnum.xml")
       test.SerializeOverride("SoapOverride.xml")
       Console.WriteLine("Fininished writing two files")
   } //mai

   private void SerializeObject(String filename
   
       // Create an instance of the XmlSerializer Class
       XmlTypeMapping mapp = (new SoapReflectionImporter())
           ImportTypeMapping(Group.class.ToType())
       XmlSerializer mySerializer = new XmlSerializer(mapp)
       // Writing the file requires a TextWriter
       TextWriter writer = new StreamWriter(filename)
       // Create an instance of the Class that will be serialized
       Group myGroup = new Group()
       // Set the object properties
       myGroup.groupName = ".NET"
       myGroup.groupType = new GroupType(GroupType.a)
       // Serialize the Class, and close the TextWriter
       mySerializer.Serialize(writer, myGroup)
       writer.Close()
   } //SerializeObjec

   private void SerializeOverride(String fileName
   
       SoapAttributeOverrides soapOver = new SoapAttributeOverrides()
       SoapAttributes SoapAtts = new SoapAttributes()
       // Add a SoapEnumAttribute for the GroupType.a enumerator.      
       // Instead of 'A'  it will be "West"
       SoapEnumAttribute soapEnum = new SoapEnumAttribute("West")
       // Override the "A" enumerator
       SoapAtts.set_SoapEnum(soapEnum)
       soapOver.Add(GroupType.class.ToType(), "A", SoapAtts)
       // Add another SoapEnumAttribute for the GroupType.B enumerator
       // Instead of //B// it will be "East"
       SoapAtts = new SoapAttributes()
       soapEnum = new SoapEnumAttribute()
       soapEnum.set_Name("East")
       SoapAtts.set_SoapEnum(soapEnum)
       soapOver.Add(GroupType.class.ToType(), "B", SoapAtts)
       // Create an XmlSerializer used for overriding
       XmlTypeMapping map = (new SoapReflectionImporter(soapOver))
           ImportTypeMapping(Group.class.ToType())
       XmlSerializer ser = new XmlSerializer(map)
       Group myGroup = new Group()
       myGroup.groupName = ".NET"
       myGroup.groupType = new GroupType(GroupType.B)
       // Writing the file requires a TextWriter
       TextWriter writer = new StreamWriter(fileName)
       ser.Serialize(writer, myGroup)
       writer.Close()
   } //SerializeOverrid
} //Ru
//</Snippet1
Lars-Inge T?nnessen - 17 May 2004 13:28 GMT
Sorry, I did not see your post until now. Please try this one.

Please see // HERE_____

import System.*;
import System.IO.*;
import System.Xml.*;
import System.Xml.Serialization.*;

public class Group
{
   public String groupName;
   public GroupType groupType;
} //Group

public class GroupType
{
   public int member;
   public GroupType()
   {
       member = 0;
   } //GroupType

   public GroupType(int n)
   {
       member = n;
   } //GroupType

   /** @attribute SoapEnum("Small") */
   public static int a = 0;

   /** @attribute SoapEnum("Large") */
   public static int B = 1;
} //GroupType

public class Run
{
   public static void main(String[] args)
   {
       Run test = new Run();
       test.SerializeObject("SoapEnum.xml");
       test.SerializeOverride("SoapOverride.xml");
       Console.WriteLine("Fininished writing two files");
   } //main

   private void SerializeObject(String filename)
   {
       TextWriter writer = new StreamWriter(filename);
       Group myGroup = new Group();
       myGroup.groupName = ".NET";
       myGroup.groupType = new GroupType(GroupType.a);

// HERE_________________
       XmlSerializer mySerializer = new XmlSerializer( myGroup.GetType() );
// ______________________

       mySerializer.Serialize(writer, myGroup);
       writer.Close();
   } //SerializeObject

   private void SerializeOverride(String fileName)
   {
       SoapAttributeOverrides soapOver = new SoapAttributeOverrides();
       SoapAttributes SoapAtts = new SoapAttributes();
       SoapEnumAttribute soapEnum = new SoapEnumAttribute("West");
       SoapAtts.set_SoapEnum(soapEnum);
       soapOver.Add(GroupType.class.ToType(), "A", SoapAtts);
       SoapAtts = new SoapAttributes();
       soapEnum = new SoapEnumAttribute();
       soapEnum.set_Name("East");
       SoapAtts.set_SoapEnum(soapEnum);
       soapOver.Add(GroupType.class.ToType(), "B", SoapAtts);
       Group myGroup = new Group();
       myGroup.groupName = ".NET";
       myGroup.groupType = new GroupType(GroupType.B);
       TextWriter writer = new StreamWriter(fileName);

// HERE_____________________
       XmlSerializer ser = new XmlSerializer( myGroup.GetType() );
// __________________________

       ser.Serialize(writer, myGroup);
       writer.Close();
   } //SerializeOverride

} //Run

Regards,
Lars-Inge T?nnessen
www.larsinge.com

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.