2016-03-22 4 views
0

私はSOAPリクエストのためにヘッダを作成する必要があります。検索後、私はXMLで私のヘッダーリクエストを生成するのに成功しましたが、私は自分のXMLタグにプレフィックスが必要です。C#でXmlRootオブジェクトの接頭辞を使用してSerialise XMLを使用するにはどうすればよいですか?

実は、私はこれを生成した:

<TimeStamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="TimeStamp-2"> 
    <Created>2016-03-22T10:10:55.710Z</Created> 
    <Expires>2016-03-22T11:10:55.710Z</Expires> 
</TimeStamp> 

そして、私はそれを持っている必要があります:私はこのトピック(How can I pass a username/password in the header to a SOAP WCF Service)のための私のクラスを作成し、私のXMLを生成するに触発された

<wsu:TimeStamp xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" wsu:Id="TimeStamp-2"> 
    <Created>2016-03-22T10:10:55.710Z</Created> 
    <Expires>2016-03-22T11:10:55.710Z</Expires> 
</wsu:TimeStamp> 

。ここで

は、私のバージョンがあります:

class SecurityHeader : MessageHeader 
{ 

    public override string Name 
    { 
     get { return "Security"; } 
    } 

    public override string Namespace 
    { 
     get { return "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"; } 
    } 

    protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
    { 
     XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
     ns.Add("wsu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"); 
     XmlSerializer serializer1 = new XmlSerializer(typeof(TimeStamp)); 
     serializer1.Serialize(Console.Out, new TimeStamp(), ns); 

    } 
} 

[XmlRoot("TimeStamp",Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd")] 
public class TimeStamp 
{ 
    public TimeStamp() 
    { 
     Id = "TimeStamp-2"; 
     Created = new Created() { Value = Created.GenerateTimeStampCreation() }; 
     Expires = new Expires() { Value = Expires.GenerateTimeStampExpiration() }; 
    } 

    [XmlAttribute(Namespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd")] 
    public string Id { get; set; } 

    [XmlElement] 
    public Created Created { get; set; } 

    [XmlElement] 
    public Expires Expires { get; set; } 
} 

答えて

0

あなたはここにいくつかの問題があります。

  1. をご<Created><Expires>要素ではなく、彼らがどんなではありませんが、"wsu"名前空間ではありません名前空間。これのXmlSerializer[XmlElement(Form = XmlSchemaForm.Unqualified)]または[XmlElement(Namespace="")]を設定することによって通知する必要があります。

  2. Id属性に明示的な名前空間を設定するには、[XmlAttribute(Form = XmlSchemaForm.Qualified)]を設定する必要があります。

  3. 名前空間に入力ミスがあります。目的のXMLには名前空間xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"がありますが、時には"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"となります。このような間違いを避けるために、文字列を複数回再入力するのではなく、正しい名前空間を含むpublic const stringを作成することができます。

このように、あなたの固定TimeStampクラスは次のようになります。

[XmlRoot("TimeStamp", Namespace = TimeStamp.WsuNamespace)] 
public class TimeStamp 
{ 
    public const string WsuNamespace = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"; 

    public TimeStamp() 
    { 
     Id = "TimeStamp-2"; 
     Created = new Created() { Value = Created.GenerateTimeStampCreation() }; 
     Expires = new Expires() { Value = Expires.GenerateTimeStampExpiration() }; 
    } 

    [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)] // Indicates this attribute is explicitly in the same namespace as the parent class 
    public string Id { get; set; } 

    [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] // Indicates this element is not in any namespace 
    public Created Created { get; set; } 

    [XmlElement(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)] // Indicates this element is not in any namespace 
    public Expires Expires { get; set; } 
} 

そして、あなたの書き込み方法では、実行します。

protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion) 
    { 
     XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); 
     ns.Add("wsu", TimeStamp.WsuNamespace); 
     XmlSerializer serializer1 = new XmlSerializer(typeof(TimeStamp)); 
     serializer1.Serialize(writer, new TimeStamp(), ns); 
    } 
+0

実は、私は自分で自分の問題を解決します。私の解決策はあなたの最初の問題で似ています。私はOnWriteHeaderContentsメソッドを使用します(ただし、このトピックでは指定しませんでした)。私はちょっと急いでいるので、週に私のメッセージを編集して、あなたの他の問題を研究し、自分のコードとヘッダーを改善してください。ありがとう@ DB2 – Adaok

関連する問題