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 は、非常に古い WSE 1.0 製品の一部である***Microsoft.***Web.Services.SoapEnvelopeクラスを使用していました。このクラスは XmlDocument クラスから派生しているため、XmlDocument でも同じ問題が発生する可能性があります。

いかなる状況においても、WSE を新しい開発に使用するべきではありません。既に使用されている場合は、できるだけ早くコードを移行する必要があります。WCF または ASP.NET Web API は、今後 .NET Web サービスに使用する必要がある唯一のテクノロジです。

于 2009-07-28T21:25:38.100 に答える
-2

2 つの表現は同等です。後者の形式で表示する必要があるのはなぜですか?

于 2008-10-31T20:23:36.260 に答える