开发者

How to ensure interoperability with DataContractSerializer when talking to non-.NET app?

开发者 https://www.devze.com 2023-01-17 16:28 出处:网络
Say I have a non-.NET app which needs to write data to be deserialized by a 开发者_高级运维.NET app via DataContractSerializer. Where\'s the specification describing the exact format necessary?

Say I have a non-.NET app which needs to write data to be deserialized by a 开发者_高级运维.NET app via DataContractSerializer. Where's the specification describing the exact format necessary?

Such a spec should describe a lot of stuff, including:

  • Does the order of sibling elements matter?
  • Should the xml namespace URIs always begin with http://schemas.datacontract.org/2004/07/?
  • do z:Id and z:Ref values need to be sequental or anything? (assuming preserveObjectReferences==true) (ok, I guess MSDN says this case is not even interoperable)
  • etc

Seems like a simple question, doesn't it? Yet I don't see it addressed directly in MSDN. (all I found was forum-posts saying the non-.NET app needs to ask the .NET app for a WSDL spec file first. But that seems wrong.)

(I don't use anything from WCF other than the DataContractSerializer)


We use the WSDL to comunicate the service definition between Java and .net applications, it works fine for us.

One thing that you need to watch out for is which datatypes you use, use those that are understood by both systems, for example:

  • If you create the service in .net, do not use datasets
  • If you create the service in java do not use vectors


The DataContractSerializer is not part of WCF, it is part of the runtime serialization which WCF is dependent on.

I have in the past used the DataContractSerializer to deserialize objects from XML I have generated out of an xml transform. This could be down the lines of what you want to do.

In order to work out what the XML required for the Serializer I found it easier to write a small piece of code that serialized my object out to a string to see how it should be structured and what the XML namespaces were.

[TestFixture]
public class TestDataContractSerializerOutput
{
    [Test]
    public void Should_give_me_some_serialized_xml()
    {
        Foo foo = new Foo();
        foo.Bars.Add(new Bar { Name = "Wibble"});
        var dataContractSerializer = new DataContractSerializer(typeof(Foo), new[] { typeof(Bar) } );

        using (Stream stream = new MemoryStream())
        {
            dataContractSerializer.WriteObject(stream, foo);
            stream.Position = 0;

            using (StreamReader streamReader = new StreamReader(stream))
            {
                Trace.WriteLine(streamReader.ReadToEnd());
            }
        }
    }
}

[DataContract]
public class Foo
{
    public Foo()
    {
        Bars = new List<IBar>();
    }

    [DataMember]
    public IList<IBar> Bars { get; set; }
}

public interface IBar
{
    string Name { get; set; }
}

[DataContract]
public class Bar : IBar
{
    public string Name { get; set; }
}

With this information you can see how to structure the XML and you can also grab the xml schemas for extra validation.

0

精彩评论

暂无评论...
验证码 换一张
取 消