Can't apply XmlType attribute when impl. IXmlSerializable
From: Daniel Lidström (someone_at_microsoft.com)
Date: 02/28/05
- Next message: James: "Inherited Class with property of same name in base class err in We"
- Previous message: Stelrad Doulton: "Re: XmlTextReader TCP Socket"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: James: "Inherited Class with property of same name in base class err in We"
- Previous message: Stelrad Doulton: "Re: XmlTextReader TCP Socket"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|