public void WriteExtensionObject(string fieldName, ExtensionObject value)
{
if (BeginField(fieldName, value == null, true))
{
PushNamespace(Namespaces.OpcUaXsd);
// check for null.
if (value == null)
{
EndField(fieldName);
PopNamespace();
return;
}
IEncodeable encodeable = value.Body as IEncodeable;
// write the type id.
ExpandedNodeId typeId = value.TypeId;
if (encodeable != null)
{
if (value.Encoding == ExtensionObjectEncoding.Binary)
{
typeId = encodeable.BinaryEncodingId;
}
else
{
typeId = encodeable.XmlEncodingId;
}
}
NodeId localTypeId = ExpandedNodeId.ToNodeId(typeId, m_context.NamespaceUris);
if (NodeId.IsNull(localTypeId) && !NodeId.IsNull(typeId))
{
if (encodeable != null)
{
throw ServiceResultException.Create(
StatusCodes.BadEncodingError,
"Cannot encode bodies of type '{0}' in ExtensionObject unless the NamespaceUri ({1}) is in the encoder's NamespaceTable.",
encodeable.GetType().FullName,
typeId.NamespaceUri);
}
localTypeId = NodeId.Null;
}
WriteNodeId("TypeId", localTypeId);
object body = value.Body;
if (body == null)
{
EndField(fieldName);
PopNamespace();
return;
}
// write the body.
m_writer.WriteStartElement("Body", Namespaces.OpcUaXsd);
WriteExtensionObjectBody(body);
// end of body.
m_writer.WriteEndElement();
EndField(fieldName);
PopNamespace();
}
}