SoapTypeAttribute (System.Xml.Serialization) Example Code Fails

From: David Bozzini (DavidBozzini_at_discussions.microsoft.com)
Date: 01/13/05


Date: Thu, 13 Jan 2005 13:43:03 -0800


[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: MVPs: static methods in interfaces. (REDEFINED)
    ... > library) every time an exception of any sort is thrown for logging ... the repeating code is not part of related classes! ... the thing that varies is this: what will we do with this string? ... public class MyEventLogger: IExceptionClass ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Soap Faults
    ... The Web Service encodes full server-side exception traces in the ... > I have had absolutely no luck working out how I can deserialize the custom ... > public string serverStackTraceId; ... > public class FdkExceptionEntry ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: JIT Debugging failed with the following error: Access is denie
    ... version of the page that throws this exception, ... 0x80004005 Unspecified error (errorid 1090) ... insufficient rights to read the .NET Framework files. ... .NET Framework is correctly installed and that the ACLs on the installation ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Exception management question...
    ... One of my very favorite books on the .NET Framework is Jeffrey Richter's ... "Applied Microsoft .NET Framework Programming." ... an excellent chapter on exception management stratgey which includes many of ... > but a stack trace should always be available. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Learning .NET Framework
    ... Much/most} of the "raw" information can be found for free online at the MSDN ... has really saved me $$ on books; the rule is that the info is in MSDN but it ... > I had a question on learning the .NET framework. ... > I assume the msdn.com website. ...
    (microsoft.public.dotnet.languages.csharp)

Loading