代码之家  ›  专栏  ›  技术社区  ›  Nicolas Dorier

为什么我用textmessageencodingbindingElement得到一个xmlException“意外的文件结尾”?

  •  2
  • Nicolas Dorier  · 技术社区  · 16 年前

    我尝试在理论上做一些非常简单的事情:将字符串反序列化为消息,下面是代码:

    [TestMethod]
    public void EncoderErrorTest()
    {
        var message = "<s:Envelope xmlns:s=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:a=\"http://www.w3.org/2005/08/addressing\"><s:Header><a:Action s:mustUnderstand=\"1\">http://tempuri.org/IHelloWorldService/SayHello</a:Action></s:Header><s:Body><SayHello xmlns=\"http://tempuri.org/\"><name>Nico</name></SayHello></s:Body></s:Envelope>";
    
        MemoryStream stream = new MemoryStream();
        StreamWriter writer = new StreamWriter(stream);
        writer.Write(message);
        stream.Position = 0;
        var soapMessage = new TextMessageEncodingBindingElement().CreateMessageEncoderFactory().Encoder.ReadMessage(stream, 99999);
        Assert.IsNotNull(soapMessage);
    }
    

    但在反序列化过程中,我得到一个xmlException“意外的文件结尾”。 我的代码有问题吗?

    提前感谢您的回复。

    1 回复  |  直到 16 年前
        1
  •  8
  •   Pavel Minaev    16 年前

    尝试呼叫 Flush 对你 StreamWriter 在您从流中阅读之前(或者更好的方法是,将它放在 using -块)。

    推荐文章