与非 .NET 应用程序交互时如何确保与 DataContractSerializer 的互操作性?

发布于 2024-09-24 04:05:46 字数 787 浏览 2 评论 0原文

假设我有一个非 .NET 应用程序,需要写入要由 .NET 应用程序通过 DataContractSerializer 反序列化的数据。 描述所需的确切格式的规范在哪里?

这样的规范应该描述很多内容,包括:

  • 同级元素的顺序重要吗?
  • xml 命名空间 URI 是否应该始终以 http://schemas.datacontract.org/2004/07/ 开头?
  • z:Id 和 z:Ref 值需要是连续的还是其他值? (假设preserveObjectReferences==true)(好吧,我猜MSDN 说这种情况甚至不能互操作

似乎是一个简单的问题,不是吗?但我没有在 MSDN 中看到直接解决这个问题。 (我发现的只是 论坛帖子说非 .NET 应用程序需要首先向 .NET 应用程序请求 WSDL 规范文件,但这似乎是错误的。)

(除了 DataContractSerializer 之外,我不使用 WCF 中的任何内容)

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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

新一帅帅 2024-10-01 04:05:46

我们使用 WSDL 在 Java 和 .net 应用程序之间传达服务定义,它对我们来说工作得很好。

您需要注意的一件事是您使用哪些数据类型,请使用两个系统都能理解的数据类型,例如:

  • 如果您在 .net 中创建服务,请勿使用数据集
  • 如果您在 java 中创建服务,请勿使用向量

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
梦冥 2024-10-01 04:05:46

DataContractSerializer 不是 WCF 的一部分,它是 WCF 所依赖的运行时序列化的一部分。

我过去曾使用 DataContractSerializer 从通过 xml 转换生成的 XML 中反序列化对象。这可能符合您想做的事情。

为了弄清楚序列化器需要什么 XML,我发现编写一小段代码将我的对象序列化为字符串以查看它的结构以及 XML 命名空间是什么会更容易。

[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; }
}

通过此信息,您可以了解如何构造 XML,还可以获取 xml 架构以进行额外验证。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文