How do I put a value between two XML tags and prefix an XML element's name using an XmlDictionaryWriter?

StackOverflow https://stackoverflow.com/questions/22457821

  •  16-06-2023
  •  | 
  •  

Domanda

I'm trying to generate the following XML:

<wsse:Security>
  <wsse:BinarySecurityToken
  ValueType="Bibble"
  EncodingType="wsse:Base64Binary"
  wsu:Id="SecurityToken ">
    Fish
  </wsse:BinarySecurityToken>
</wsse:Security>

The code I have is:

protected override void OnWriteStartHeader(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
    writer.WriteStartElement("wsse", "Security" ,Namespace);
    writer.WriteXmlnsAttribute("wsse", Namespace);
}

protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
    writer.WriteStartElement("wsse", "BinarySecurityToken", Namespace);
    writer.WriteAttributeString("ValueType", "Bibble");
    writer.WriteAttributeString("EncodingType", "wsse:Base64Binary");
    writer.WriteEndElement();
}

Problem 1: I can't figure out how to put the value of Fish between the start and end tags.

Problem 2: I can't figure out how to get the wsu:Id element, as this code generates an exception: writer.WriteAttributeString("wsu:Id", "SecurityToken");

Any ideas?

È stato utile?

Soluzione

Problem 1:

I can't seem to find XmlDictionaryWriter, but I believe you need writer.WriteValue("Fish");

Problem 2:

Utilize one of the other overloads:

writer.WriteAttributeString("wsu", "Id", wsu_namespace_string, "SecurityToken");

where wsu_namespace_string contains the namespace declaration for the WSU prefix

Altri suggerimenti

In OnWriteHeaderContents(), before your call to writer.WriteEndElement();, add this:

writer.WriteValue("Fish");

Per MSDN, WriteValue will take a string and simply write it out. There are other overloads for many different CLR types and object.

So, OnWriteHeaderContents() would look like this:

protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
    writer.WriteStartElement("wsse", "BinarySecurityToken", Namespace);
    writer.WriteAttributeString("ValueType", "Bibble");
    writer.WriteAttributeString("EncodingType", "wsse:Base64Binary");
    writer.WriteValue("Fish");//write Fish
    writer.WriteEndElement();
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top