protected virtual XacmlContextAttribute ReadContextAttribute(XmlReader reader) { if (reader == null) { throw new ArgumentNullException(nameof(reader)); } if (!reader.IsStartElement(XacmlConstants.ElementNames.Attribute, this.Version.NamespaceContext)) { throw ThrowHelperXml(reader, "XML message is not valid."); } // Read attributes Uri attributeId = this.ReadAttribute <Uri>(reader, XacmlConstants.AttributeNames.AttributeId, isRequered: true); Uri dataType = this.ReadAttribute <Uri>(reader, XacmlConstants.AttributeNames.DataType, isRequered: true); string issuer = this.ReadAttribute <string>(reader, XacmlConstants.AttributeNames.Issuer, isRequered: false); DateTime?issueInstant = null; string issueInstantString = this.ReadAttribute <string>(reader, XacmlConstants.AttributeNames.IssueInstant, isRequered: false); if (!string.IsNullOrWhiteSpace(issueInstantString)) { issueInstant = XmlConvert.ToDateTime(issueInstantString, XmlDateTimeSerializationMode.Local); } reader.ReadStartElement(XacmlConstants.ElementNames.Attribute, this.Version.NamespaceContext); // Read elements XacmlContextAttributeValue attrVal = this.ReadRequired(XacmlConstants.ElementNames.AttributeValue, this.Version.NamespaceContext, this.ReadContextAttributeValue, reader); XacmlContextAttribute result = new XacmlContextAttribute(attributeId, dataType, attrVal) { Issuer = issuer, IssueInstant = issueInstant, }; reader.ReadEndElement(); return(result); }
protected virtual void WriteContextAttribute(XmlWriter writer, XacmlContextAttribute attr) { if (writer == null) { throw new ArgumentNullException(nameof(writer)); } if (attr == null) { throw new ArgumentNullException(nameof(attr)); } writer.WriteStartElement(XacmlConstants.Prefixes.Context, XacmlConstants.ElementNames.Attribute, this.Version.NamespaceContext); writer.WriteAttributeString(XacmlConstants.AttributeNames.AttributeId, attr.AttributeId.ToString()); writer.WriteAttributeString(XacmlConstants.AttributeNames.DataType, attr.DataType.ToString()); if (!string.IsNullOrEmpty(attr.Issuer)) { writer.WriteAttributeString(XacmlConstants.AttributeNames.Issuer, attr.Issuer); } if (attr.IssueInstant != null) { writer.WriteAttributeString(XacmlConstants.AttributeNames.IssueInstant, attr.IssueInstant.ToString()); } if (attr.AttributeValues.Count > 1) { throw new InvalidOperationException("AttributeValues shoul be 1 in version 1.0"); } this.WriteContextAttributeValue(writer, attr.AttributeValues.First()); writer.WriteEndElement(); }