PC Review


Reply
Thread Tools Rate Thread

Can't call WriteXml on a class implementing IXmlSerializable

 
 
=?Utf-8?B?RmxvcmVudCBQaWxsZXQ=?=
Guest
Posts: n/a
 
      16th Nov 2005
Maybe I'm trying to do things in a counter fashion. I created a class
implementing the System.Xml.XmlSerialization.IXmlSerializable interface. When
trying to compile code that explicitely calls my class's WriteXml method, the
compiler tells me that the class doesn't have such a method.

I'm using Visual Studio 2005 Beta 2 and was trying to do this with either
..NET 2.0 or .NETCF 2.0. Neither worked.

Any help on this ? Here is some sample code that doesn't compile (I get an
error: 'WindowsApplication1.Class1' does not contain a definition for
'WriteXml'):

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

namespace WindowsApplication1
{
public class Class1 : IXmlSerializable
{
XmlSchema IXmlSerializable.GetSchema() {
return null;
}

void IXmlSerializable.ReadXml(XmlReader reader) {
reader.ReadStartElement("MyClass");
reader.ReadEndElement();
}

void IXmlSerializable.WriteXml(XmlWriter writer) {
writer.WriteStartElement("MyClass");
writer.WriteEndElement();
}
}

static class Program
{
[STAThread]
static void Main()
{
Class1 test;
TextWriter writer = new StreamWriter("test.xml");
XmlTextWriter w = new XmlTextWriter(writer);
w.WriteStartDocument();
w.WriteStartElement("Tests");
test.WriteXml(w);
}
}
}
 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGF2aWQgQw==?=
Guest
Posts: n/a
 
      17th Nov 2005
You've created an explicit interface implementation by prefixing the method
name with the interface name. Either remove the interface name from the
method definition or cast the object to IXmlSerializable then call the method.

DC

"Florent Pillet" wrote:

> Maybe I'm trying to do things in a counter fashion. I created a class
> implementing the System.Xml.XmlSerialization.IXmlSerializable interface. When
> trying to compile code that explicitely calls my class's WriteXml method, the
> compiler tells me that the class doesn't have such a method.
>
> I'm using Visual Studio 2005 Beta 2 and was trying to do this with either
> .NET 2.0 or .NETCF 2.0. Neither worked.
>
> Any help on this ? Here is some sample code that doesn't compile (I get an
> error: 'WindowsApplication1.Class1' does not contain a definition for
> 'WriteXml'):
>
> using System;
> using System.Xml;
> using System.Xml.Schema;
> using System.Xml.Serialization;
> using System.IO;
>
> namespace WindowsApplication1
> {
> public class Class1 : IXmlSerializable
> {
> XmlSchema IXmlSerializable.GetSchema() {
> return null;
> }
>
> void IXmlSerializable.ReadXml(XmlReader reader) {
> reader.ReadStartElement("MyClass");
> reader.ReadEndElement();
> }
>
> void IXmlSerializable.WriteXml(XmlWriter writer) {
> writer.WriteStartElement("MyClass");
> writer.WriteEndElement();
> }
> }
>
> static class Program
> {
> [STAThread]
> static void Main()
> {
> Class1 test;
> TextWriter writer = new StreamWriter("test.xml");
> XmlTextWriter w = new XmlTextWriter(writer);
> w.WriteStartDocument();
> w.WriteStartElement("Tests");
> test.WriteXml(w);
> }
> }
> }

 
Reply With Quote
 
=?Utf-8?B?RmxvcmVudCBQaWxsZXQ=?=
Guest
Posts: n/a
 
      17th Nov 2005
Ah that makes sense now! Looks like I missed this subtlety when checking the
C# docs. Thanks.

"David C" wrote:

> You've created an explicit interface implementation by prefixing the method
> name with the interface name. Either remove the interface name from the
> method definition or cast the object to IXmlSerializable then call the method.
>
> DC


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IXmlSerializable - Deserialize class with class references Jamie Microsoft C# .NET 0 31st Jul 2006 07:26 PM
ReadXML not being called when implementing IXmlSerializable =?Utf-8?B?TWFyZWs=?= Microsoft C# .NET 0 22nd Jun 2006 08:20 AM
call a mnuFileNew_Click method from a Main MDI class from another class (can be a MdiChild class.. )? M. G, Microsoft Dot NET Framework Forms 1 31st May 2006 06:28 AM
Issues Serializing A Derived Class and its Base using IXmlSerializable, Can't See Base Peter Nofelt Microsoft Dot NET 1 10th Feb 2006 09:30 PM
abstract class, implementing virtual class loaded at runtime Robb Sadler Microsoft C# .NET 2 11th Oct 2003 03:58 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:35 AM.