N
Nils Gösche
Hi!
Is the DataContractSerializer supposed to work with an XmlSchemaSet
object? I am having problems getting them to work together. Here is some
code that illustrates the problem.
First, we define a type Blark that is used to generate an XmlSchemaSet
using the XsdDataContractExporter. The type HasSchema is a type that
contains an XmlSchemaSet as DataMember. We create an instance of
HasSchema with the above generated XmlSchemaSet. Then we serialize and
deserialize this instance using the DataContractSerializer. Before
serialization, the XmlSchemaSet has Count = 3, but after serialization,
Count = 0, so apparently, the set is empty. No exception is generated.
Am I doing something wrong here? Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Runtime.Serialization;
namespace ConsoleTest
{
[DataContract]
class Blark
{
[DataMember]
public int Foo { get; private set; }
public Blark(int foo)
{
Foo = foo;
}
}
[DataContract]
class HasSchema
{
[DataMember]
public XmlSchemaSet TheSchema { get; private set; }
public HasSchema(XmlSchemaSet theSchema)
{
TheSchema = theSchema;
}
}
class Program
{
static byte[] SerializeSchemas(HasSchema schemas)
{
var serializer = new DataContractSerializer(typeof(HasSchema));
var ms = new MemoryStream();
var writer = XmlDictionaryWriter.CreateBinaryWriter(ms);
serializer.WriteObject(writer, schemas);
writer.Flush();
return ms.ToArray();
}
static HasSchema DeserializeSchemas(byte[] buf)
{
var reader = XmlDictionaryReader.CreateBinaryReader(buf, XmlDictionaryReaderQuotas.Max);
var parser = new DataContractSerializer(typeof(HasSchema));
return (HasSchema) parser.ReadObject(reader);
}
static HasSchema TestSchemas(HasSchema schemas)
{
return DeserializeSchemas(SerializeSchemas(schemas));
}
static void Main(string[] args)
{
var exporter = new XsdDataContractExporter();
exporter.Export(typeof(Blark));
exporter.Schemas.Compile();
var beforeSerializing = new HasSchema(exporter.Schemas);
var afterSerializing = TestSchemas(beforeSerializing);
Console.WriteLine("{0} schemas before serializing", beforeSerializing.TheSchema.Count);
Console.WriteLine("{0} schemas after serializing", afterSerializing.TheSchema.Count);
Console.ReadKey();
}
}
}
Regards,
Is the DataContractSerializer supposed to work with an XmlSchemaSet
object? I am having problems getting them to work together. Here is some
code that illustrates the problem.
First, we define a type Blark that is used to generate an XmlSchemaSet
using the XsdDataContractExporter. The type HasSchema is a type that
contains an XmlSchemaSet as DataMember. We create an instance of
HasSchema with the above generated XmlSchemaSet. Then we serialize and
deserialize this instance using the DataContractSerializer. Before
serialization, the XmlSchemaSet has Count = 3, but after serialization,
Count = 0, so apparently, the set is empty. No exception is generated.
Am I doing something wrong here? Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Runtime.Serialization;
namespace ConsoleTest
{
[DataContract]
class Blark
{
[DataMember]
public int Foo { get; private set; }
public Blark(int foo)
{
Foo = foo;
}
}
[DataContract]
class HasSchema
{
[DataMember]
public XmlSchemaSet TheSchema { get; private set; }
public HasSchema(XmlSchemaSet theSchema)
{
TheSchema = theSchema;
}
}
class Program
{
static byte[] SerializeSchemas(HasSchema schemas)
{
var serializer = new DataContractSerializer(typeof(HasSchema));
var ms = new MemoryStream();
var writer = XmlDictionaryWriter.CreateBinaryWriter(ms);
serializer.WriteObject(writer, schemas);
writer.Flush();
return ms.ToArray();
}
static HasSchema DeserializeSchemas(byte[] buf)
{
var reader = XmlDictionaryReader.CreateBinaryReader(buf, XmlDictionaryReaderQuotas.Max);
var parser = new DataContractSerializer(typeof(HasSchema));
return (HasSchema) parser.ReadObject(reader);
}
static HasSchema TestSchemas(HasSchema schemas)
{
return DeserializeSchemas(SerializeSchemas(schemas));
}
static void Main(string[] args)
{
var exporter = new XsdDataContractExporter();
exporter.Export(typeof(Blark));
exporter.Schemas.Compile();
var beforeSerializing = new HasSchema(exporter.Schemas);
var afterSerializing = TestSchemas(beforeSerializing);
Console.WriteLine("{0} schemas before serializing", beforeSerializing.TheSchema.Count);
Console.WriteLine("{0} schemas after serializing", afterSerializing.TheSchema.Count);
Console.ReadKey();
}
}
}
Regards,