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 / .NET Framework / New Users / January 2005

Tip: Looking for answers? Try searching our database.

SoapTypeAttribute (System.Xml.Serialization) Example Code Fails

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
David Bozzini - 13 Jan 2005 21:43 GMT
[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.

Signature

David Bozzini
Susquehanna International Group

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

> [This is a repost, never got an answer the first time, 12/17/2004]
>
[quoted text clipped - 137 lines]
>
> Thanks.
David Bozzini - 14 Jan 2005 12:41 GMT
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlr
fsystemxmlserializationsoaptypeattributeclasstopic.asp


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

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.