public override void WriteStartAttribute(string prefix, string localName, string ns)
{
if (IsClosed)
{
ThrowClosed();
}
if (!string.IsNullOrEmpty(prefix))
{
if (IsWritingNameWithMapping && prefix == JsonGlobals.xmlnsPrefix)
{
if (ns != null && ns != xmlnsNamespace)
{
throw new ArgumentException(SR.Format(SR.XmlPrefixBoundToNamespace, "xmlns", xmlnsNamespace, ns), nameof(ns));
}
}
else
{
throw new ArgumentException(SR.Format(SR.JsonPrefixMustBeNullOrEmpty, prefix), nameof(prefix));
}
}
else
{
if (IsWritingNameWithMapping && ns == xmlnsNamespace && localName != JsonGlobals.xmlnsPrefix)
{
prefix = JsonGlobals.xmlnsPrefix;
}
}
if (!string.IsNullOrEmpty(ns))
{
if (IsWritingNameWithMapping && ns == xmlnsNamespace)
{
prefix = JsonGlobals.xmlnsPrefix;
}
else if (string.IsNullOrEmpty(prefix) && localName == JsonGlobals.xmlnsPrefix && ns == xmlnsNamespace)
{
prefix = JsonGlobals.xmlnsPrefix;
_isWritingXmlnsAttributeDefaultNs = true;
}
else
{
throw new ArgumentException(SR.Format(SR.JsonNamespaceMustBeEmpty, ns), nameof(ns));
}
}
if (localName == null)
{
throw new ArgumentNullException(nameof(localName));
}
if (localName.Length == 0)
{
throw new ArgumentException(SR.JsonInvalidLocalNameEmpty, nameof(localName));
}
if ((_nodeType != JsonNodeType.Element) && !_wroteServerTypeAttribute)
{
throw new XmlException(SR.JsonAttributeMustHaveElement);
}
if (HasOpenAttribute)
{
throw new XmlException(SR.Format(SR.JsonOpenAttributeMustBeClosedFirst, "WriteStartAttribute"));
}
if (prefix == JsonGlobals.xmlnsPrefix)
{
_isWritingXmlnsAttribute = true;
}
else if (localName == JsonGlobals.typeString)
{
if (_dataType != JsonDataType.None)
{
throw new XmlException(SR.Format(SR.JsonAttributeAlreadyWritten, JsonGlobals.typeString));
}
_isWritingDataTypeAttribute = true;
}
else if (localName == JsonGlobals.serverTypeString)
{
if (_serverTypeValue != null)
{
throw new XmlException(SR.Format(SR.JsonAttributeAlreadyWritten, JsonGlobals.serverTypeString));
}
if ((_dataType != JsonDataType.None) && (_dataType != JsonDataType.Object))
{
throw new XmlException(SR.Format(SR.JsonServerTypeSpecifiedForInvalidDataType,
JsonGlobals.serverTypeString, JsonGlobals.typeString, _dataType.ToString().ToLowerInvariant(), JsonGlobals.objectString));
}
_isWritingServerTypeAttribute = true;
}
else if (localName == JsonGlobals.itemString)
{
if (WrittenNameWithMapping)
{
throw new XmlException(SR.Format(SR.JsonAttributeAlreadyWritten, JsonGlobals.itemString));
}
if (!IsWritingNameWithMapping)
{
// Don't write attribute with local name "item" if <item> element is not open.
// Not providing a better error message because localization deadline has passed.
throw new XmlException(SR.JsonEndElementNoOpenNodes);
}
_nameState |= NameState.IsWritingNameAttribute;
}
else
{
throw new ArgumentException(SR.Format(SR.JsonUnexpectedAttributeLocalName, localName), nameof(localName));
}
}