Home All Groups Group Topic Archive Search About

SoapTypeAttribute (System.Xml.Serialization) Example Code Fails

Author
13 Jan 2005 9:43 PM
David Bozzini
[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

Author
14 Jan 2005 8:35 AM
R. Thomas, aka Xtreme.Net
Can u gimme a link where this code is posted so that i can go and take a look?
Thanks
R. Thomas

Show quote
"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
Author
14 Jan 2005 12:41 PM
David Bozzini
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemxmlserializationsoaptypeattributeclasstopic.asp

Show quote
"R. Thomas, aka Xtreme.Net" wrote:

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

AddThis Social Bookmark Button