Can't apply XmlType attribute when impl. IXmlSerializable

From: Daniel Lidström (someone_at_microsoft.com)
Date: 02/28/05


Date: Mon, 28 Feb 2005 08:59:34 +0100

Hello!

I can't apply the XmlType attribute when I implement IXmlSerializable.
There is an exception being thrown:

Unhandled Exception: System.InvalidOperationException: There was an error
generating the XML document. ---> System.InvalidOperationException: There
was an error reflecting type 'Start'. --->
System.InvalidOperationException: XML attributes may not be specified for
the type Start.

The problem is, that without specifying XmlType, then I get this xml
output:

<?xml version="1.0" encoding="ibm850"?>
<LandXml xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="LandXML">
  <Line line="line">
    <Start start="start" xmlns="">
      <a>7</a>
      <b>4</b>
      <c>0</c>name</Start>
  </Line>
</LandXml>

Here, the element Start is within a default namespace. I want it to be
within the LandXML namespace, so that the xml would be:

<?xml version="1.0" encoding="ibm850"?>
<LandXml xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="LandXML">
  <Line line="line">
    <Start start="start">
      <a>7</a>
      <b>4</b>
      <c>0</c>name</Start>
  </Line>
</LandXml>

Here is the (minimal) code I'm working on. The XmlType attribute for the
class Start has been commented out. Try uncommenting it.

using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Schema;

//[XmlType(Namespace="LandXML")]
public class Start : Point3D
{
}

public class End : Point3D
{
}

public class Point3D : Point2D
{
   public double c = rnd.Next(10);

   public override void WriteXml(XmlWriter w)
   {
      w.WriteAttributeString("start", s);
      w.WriteElementString("a", null, a.ToString());
      w.WriteElementString("b", null, b.ToString());
      w.WriteElementString("c", null, c.ToString());
      w.WriteChars(Name.ToCharArray(), 0, Name.Length);
   }

   public override XmlSchema GetSchema()
   {
      // TODO: Add Start.GetSchema implementation
      return null;
   }

   public override void ReadXml(XmlReader reader)
   {
      // TODO: Add Start.ReadXml implementation
   }
}

public class Point2D : IXmlSerializable
{
   public static Random rnd = new Random();
   public double a = rnd.Next(10), b = rnd.Next(10);

   [XmlAttribute]
   public string s = "start";

   [XmlText]
   public string Name
   {
      get { return "name"; }
      set { }
   }

   public virtual void WriteXml(XmlWriter w)
   {
      w.WriteAttributeString("start", s);
      w.WriteElementString("a", null, a.ToString());
      w.WriteElementString("b", null, b.ToString());
      w.WriteChars(Name.ToCharArray(), 0, Name.Length);
   }

   public virtual XmlSchema GetSchema()
   {
      // TODO: Add Start.GetSchema implementation
      return null;
   }

   public virtual void ReadXml(XmlReader reader)
   {
      // TODO: Add Start.ReadXml implementation
   }
}

public class Line : IXmlSerializable
{
   [XmlAttribute]
   public string s = "line";
   public Start start = new Start();
   public void WriteXml(XmlWriter w)
   {
      w.WriteAttributeString("line", s);
      //! serialize start
      XmlSerializer ser = new XmlSerializer(typeof(Start));
      ser.Serialize(w, start);
   }

   public void ReadXml(XmlReader r)
   {
   }

   public XmlSchema GetSchema()
   {
      return null;
   }
}

[XmlRoot(Namespace="LandXML")]
public class LandXml
{
   public LandXml() { line = new Line(); }

   [XmlElement(ElementName="Line")]
   public Line line;
}

public class Test
{
   public static void Main()
   {
      Test t = new Test();
   }

   public Test()
   {
      Console.WriteLine("** Serializing LandXml:");
      XmlSerializer ser = new XmlSerializer(typeof(LandXml));
      ser.Serialize(Console.Out, new LandXml());
      Console.Write("\n");
   }
}

I realize this is an advanced question, since even implementing
IXmlSerializable is unsupported. I would be really interested in knowing if
this works with .Net Framework 2.0. Any of the MVPs know anything?
Thanks in advance!

-- 
Daniel


Relevant Pages

  • Re: Help with my first Java program
    ... Metto go = new Metto; ... public void windowClosing ... private void populateMettoPane() ... public class press1 extends JPanel ...
    (comp.lang.java.programmer)
  • more silly questions that are probably elementary to most but.....
    ... public static void main{ ... Wolf aWolf = new Wolf; ... Cat c = new Cat; ... public class Cat extends Feline { ...
    (comp.lang.java.help)
  • Re: overriding inner classes
    ... > public class ClassA{ ... > public void doit(){ ... > public class ClassB extends ClassA{ ...
    (comp.lang.java.programmer)
  • Re: Logic Question about Public Method I created.
    ... public class clsUserControl ... foreach(ListDictionary MyListRow in MyListDictionary) ... private void DoSomething() ...
    (microsoft.public.dotnet.framework.windowsforms)
  • defining objects with xml/schema
    ... represent the classes in xml and auto-generate them ... will be hard for other people who don't have a grasp of xsd schema. ... indicates a code generator option. ... public class item2Object ...
    (microsoft.public.dotnet.languages.csharp)