How to derive from base implementing IXmlSerializable?

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello,
I am stuck trying to derive from a base class implementing IXmlSerializable. Marking a property in the subclass with [XmlElement] does not serialize the property. What I want is a base class implementing IXmlSerializable for custom serialization
but when derived from it should work as an "ordinary class".
The problem probably lies in my implementation of [XmlSchemaProvider] or maybe what i want to do is not doable?
I have isolated the code to the following:

<pre class="prettyprint [XmlSchemaProvider("GetXmlSchemaType")]
public class BaseClass: IXmlSerializable
{
public string BaseName { get; set; }
// This is the method named by the XmlSchemaProviderAttribute applied to the type.
public static XmlSchemaType GetXmlSchemaType(XmlSchemaSet xs)
{
// This method is called by the framework to get the schema for this type.
// We return an existing schema from disk.
if(xs.Count!=0)
{
throw new NotImplementedException();
//XmlSerializer schemaSerializer = new XmlSerializer(typeof(XmlSchema));
//string xsdPath = null;
//// NOTE: replace the string with your own path.
//xsdPath = System.Web.HttpContext.Current.Server.MapPath("SongStream.xsd");
//XmlSchema s = (XmlSchema)schemaSerializer.Deserialize(new XmlTextReader(xsdPath), null);
//xs.XmlResolver = new XmlUrlResolver();
//xs.Add(s);
}
return new XmlSchemaType();
}

public XmlSchema GetSchema()
{
throw new NotImplementedException();
}

private const string BaseElementName = "BaseName";

public void ReadXml(XmlReader reader)
{
this.BaseName = reader.ReadElementString(BaseElementName);
}

public void WriteXml(XmlWriter writer)
{
writer.WriteElementString(BaseElementName,BaseName);
}
}[/code]
<br/>
<pre class="prettyprint public class DerivedClass : BaseClass
{
[XmlElement]
public string DerivedName { get; set; }
}[/code]
<pre class="prettyprint [TestFixture]
public class XmlSerializableTest
{
[Test]
public void SerializeBaseClassTest()
{
var baseClass = new BaseClass() {BaseName = "mr Base"};
var actual = SerializeToString(baseClass);
Assert.AreEqual(@"<?xml version=""1.0"" encoding=""utf-16""?><BaseClass><BaseName>mr Base</BaseName></BaseClass>", actual);
}

[Test]
public void SerializeDerivedClassTest()
{
var derivedClass = new DerivedClass() { BaseName = "mr Base",DerivedName = "mr Derived"};
var actual = SerializeToString(derivedClass);
Assert.AreNotEqual(@"<?xml version=""1.0"" encoding=""utf-16""?><DerivedClass><BaseName>mr Base</BaseName></DerivedClass>", actual);
}

private static string SerializeToString<T>(T item)
{
var xmlSerializer = new XmlSerializer(item.GetType());
using (var stringWriter = new StringWriter())
{
using (var xmlTextWriter = new XmlTextWriter(stringWriter))
{
xmlSerializer.Serialize(xmlTextWriter, item);
var xml = stringWriter.ToString();
return xml;
}
}
}
}[/code]
<br/>
<br/>
<hr class="sig Trying to learn

View the full article
 
Back
Top