5

我希望能够将序列化的 xml 类加载到 Soap Envelope。我开始了,所以我没有填充内脏,所以它看起来像:

<Envelope    
xmlns="http://schemas.xmlsoap.org/soap/envelope/" /> 

我希望它看起来像:

<Envelope    
xmlns="http://schemas.xmlsoap.org/soap/envelope/" ></Envelope>`


我写的课是这样的:

[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/",ElementName="Envelope", IsNullable = true)]
public class TestXmlEnvelope
{
  [System.Xml.Serialization.XmlElement(ElementName="Body", Namespace="http://schemas.xmlsoap.org/soap/envelope/")]
  public System.Collections.ArrayList Body = new System.Collections.ArrayList();
} //class TestXmlEnvelope`

我以这个为例,因为其他人可能想要它在一个单独的元素中。我确信这一定很简单,但遗憾的是我不知道正确的关键字。

一如既往地感谢您的帮助。

[编辑] 当我尝试使用此指令时出现错误

System.Xml.Serialization.XmlSerializer xmlout = new System.Xml.Serialization.XmlSerializer(typeof(TestXmlEnvelope));
System.IO.MemoryStream memOut = new System.IO.MemoryStream();
xmlout.Serialize(memOut, envelope, namespc);
Microsoft.Web.Services.SoapEnvelope soapEnv = new Microsoft.Web.Services.SoapEnvelope();
soapEnv.Load(memOut);

它给了我错误“找不到根元素”。

[编辑]我修复了错误,问题是在我序列化对象后我没有设置memOut.Position = 0。我仍然希望这个问题可以帮助其他可能想要这样做的人。

4

3 回答 3

11

这里的主要问题是对何时写入结束标签的XmlSerializer调用。但是,这会在没有内容时生成速记形式。分别写结束标签。WriteEndElement()XmlWriter<tag/>WriteFullEndElement()

您可以将自己的XmlTextWriter代码注入到序列化程序用来展示该功能的中间。

鉴于这serializer是适当的XmlSerializer,试试这个:

public class XmlTextWriterFull : XmlTextWriter
{
    public XmlTextWriterFull(TextWriter sink) : base(sink) { }

    public override void WriteEndElement()
    {
        base.WriteFullEndElement();
    }
}

...

var writer = new XmlTextWriterFull(innerwriter);
serializer.Serialize(writer, obj);

[编辑]对于您添加代码的情况,添加外观构造函数:

public XmlTextWriterFull(Stream stream, Encoding enc) : base(stream, enc) { }
public XmlTextWriterFull(String str, Encoding enc) : base(str, enc) { }

然后,像以前一样在构造函数中使用内存流作为内部流:

System.IO.MemoryStream memOut = new System.IO.MemoryStream();
XmlTextWriterFull writer = new XmlTextWriterFull(memOut, Encoding.UTF8Encoding); //Or the encoding of your choice
xmlout.Serialize(writer, envelope, namespc);
于 2008-10-31T20:44:10.027 回答
1

注意记录:OP 使用的是***Microsoft.***Web.Services.SoapEnvelope类,它是非常过时的 WSE 1.0 产品的一部分。此类派生自 XmlDocument 类,因此 XmlDocument 可能会出现相同的问题。

在任何情况下都不应将 WSE 用于任何新的开发,如果它已经在使用,则应尽快迁移代码。WCF 或 ASP.NET Web API 是未来应该用于 .NET Web 服务的唯一技术。

于 2009-07-28T21:25:38.100 回答
-2

这两种表示是等价的。为什么需要它以后一种形式出现?

于 2008-10-31T20:23:36.260 回答