RE: SoapTypeAttribute (System.Xml.Serialization) Example Code Fails

From: R. Thomas, aka Xtreme.Net (removemeXtremeDotNet_at_hotmail.com.NoSpam)
Date: 01/14/05


Date: Fri, 14 Jan 2005 00:35:03 -0800

Can u gimme a link where this code is posted so that i can go and take a look?
Thanks
R. Thomas

"David Bozzini" wrote:

> [This is a repost, never got an answer the first time, 12/17/2004]
>
> This is for Microsoft .NET Framework 1.1.
>
> The example code given in the .NET Framework Class Library MSDN documentation
> for SoapTypeAttribute Class [C#] compiles but fails at runtime. Can someone
> possibly offer a fix, or a workaround? We need a fix or workaround in order
> to proceed with using SoapTypeAttribute in our own code.
>
> Here is the code, copied verbatim from the MSDN docs. When compiled as a
> .NET Console application and run, it throws an exception like so:
>
> [begin exception text]
> An unhandled exception of type 'System.InvalidOperationException' occurred
> in system.xml.dll
>
> Additional information: There was an error generating the XML document.
> [end exception text]
>
> [begin MSDN example code]
>
> using System;
> using System.IO;
> using System.Xml;
> using System.Xml.Serialization;
>
> // The SoapType is overridden when the
> // SerializeOverride method is called.
> [SoapType("SoapGroupType", "http://www.cohowinery.com")]
> public class Group
> {
> public string GroupName;
> public Employee[] Employees;
> }
>
> [SoapType("EmployeeType")]
> public class Employee{
> public string Name;
> }
>
> public class Run
> {
> public static void Main()
> {
> Run test = new Run();
> test.SerializeOriginal("SoapType.xml");
> test.SerializeOverride("SoapType2.xml");
> test.DeserializeObject("SoapType2.xml");
> }
>
> public void SerializeOriginal(string filename){
> // Create an instance of the XmlSerializer class that
> // can be used for serializing as a SOAP message.
> XmlTypeMapping mapp =
> (new SoapReflectionImporter()).ImportTypeMapping(typeof(Group));
> 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";
> Employee e1 = new Employee();
> e1.Name = "Pat";
> myGroup.Employees=new Employee[]{e1};
>
> // Serialize the class, and close the TextWriter.
> mySerializer.Serialize(writer, myGroup);
> writer.Close();
> }
>
>
> public void SerializeOverride(string filename)
> {
> // Create an instance of the XmlSerializer class that
> // uses a SoapAttributeOverrides object.
>
> XmlSerializer mySerializer = CreateOverrideSerializer();
>
> // 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";
> Employee e1 = new Employee();
> e1.Name = "Pat";
> myGroup.Employees=new Employee[]{e1};
>
> // Serialize the class, and close the TextWriter.
> mySerializer.Serialize(writer, myGroup);
> writer.Close();
> }
>
> private XmlSerializer CreateOverrideSerializer()
> {
> // Create and return an XmlSerializer instance used to
> // override and create SOAP messages.
> SoapAttributeOverrides mySoapAttributeOverrides =
> new SoapAttributeOverrides();
> SoapAttributes soapAtts = new SoapAttributes();
>
> // Override the SoapTypeAttribute.
> SoapTypeAttribute soapType = new SoapTypeAttribute();
> soapType.TypeName = "Team";
> soapType.IncludeInSchema = false;
> soapType.Namespace = "http://www.microsoft.com";
> soapAtts.SoapType = soapType;
>
> mySoapAttributeOverrides.Add(typeof(Group),soapAtts);
>
> // Create an XmlTypeMapping that is used to create an instance
> // of the XmlSerializer. Then return the XmlSerializer object.
> XmlTypeMapping myMapping = (new SoapReflectionImporter(
> mySoapAttributeOverrides)).ImportTypeMapping(typeof(Group));
>
> XmlSerializer ser = new XmlSerializer(myMapping);
> return ser;
> }
>
> public void DeserializeObject(string filename){
> // Create an instance of the XmlSerializer class.
> XmlSerializer mySerializer = CreateOverrideSerializer();
> // Reading the file requires a TextReader.
> TextReader reader = new StreamReader(filename);
>
> // Deserialize and cast the object.
> Group myGroup;
> myGroup = (Group) mySerializer.Deserialize(reader);
>
> Console.WriteLine(myGroup.GroupName);
> }
> }
> [end MSDN example code]
>
> Thanks.
>
> --
> David Bozzini
> Susquehanna International Group



Relevant Pages

  • Re: How to use generics?
    ... getting rid of the ACollection and BCollection and just having Collectionwould be a good start towards using generics to the full extent. ... public void AddCollection() ... public bool ContainsKey ...
    (microsoft.public.dotnet.languages.csharp)
  • program challenge
    ... public void setName{ ... the salaried class that extends Employee ... public class Salaried extends Employee ... // Programmer.java: the programmer class that extends from Hourly ...
    (comp.lang.java.help)
  • Re: Tricky form problem
    ... > ShowDialog method) different modal child forms. ... when using ShowDialog() there's no ... > public class MainForm: Form ... > public void ShowForm() ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: InvalidOperationException was unhandled
    ... > public Vehicle MyVehicle; ... >public class Vehicle ... > public void SerializeOriginal(String filename) ...
    (microsoft.public.dotnet.vjsharp)
  • RE: Problem: C# custom event is null
    ... public event MyDelegate OnFire; ... public void B_Event_Raised ... (these are sample classes to illustrate) ...
    (microsoft.public.dotnet.languages.csharp)