Exemple #1
0
        private void ReadElement(
            JsonReader reader,
            IXmlDocument document,
            IXmlNode currentNode,
            string propertyName,
            XmlNamespaceManager manager)
        {
            if (string.IsNullOrEmpty(propertyName))
            {
                throw JsonSerializationException.Create(reader,
                                                        "XmlNodeConverter cannot convert JSON with an empty property name to XML.");
            }
            Dictionary <string, string> attributeNameValues = this.ReadAttributeElements(reader, manager);
            string prefix1 = MiscellaneousUtils.GetPrefix(propertyName);

            if (propertyName.StartsWith('@'))
            {
                string str     = propertyName.Substring(1);
                string prefix2 = MiscellaneousUtils.GetPrefix(str);
                XmlNodeConverter.AddAttribute(reader, document, currentNode, propertyName, str, manager, prefix2);
            }
            else
            {
                if (propertyName.StartsWith('$'))
                {
                    if (!(propertyName == "$values"))
                    {
                        if (propertyName == "$id" || propertyName == "$ref" ||
                            (propertyName == "$type" || propertyName == "$value"))
                        {
                            string attributeName   = propertyName.Substring(1);
                            string attributePrefix = manager.LookupPrefix("http://james.newtonking.com/projects/json");
                            XmlNodeConverter.AddAttribute(reader, document, currentNode, propertyName, attributeName, manager,
                                                          attributePrefix);
                            return;
                        }
                    }
                    else
                    {
                        propertyName = propertyName.Substring(1);
                        string elementPrefix = manager.LookupPrefix("http://james.newtonking.com/projects/json");
                        this.CreateElement(reader, document, currentNode, propertyName, manager, elementPrefix,
                                           attributeNameValues);
                        return;
                    }
                }

                this.CreateElement(reader, document, currentNode, propertyName, manager, prefix1, attributeNameValues);
            }
        }
Exemple #2
0
        private void SerializeNode(
            JsonWriter writer,
            IXmlNode node,
            XmlNamespaceManager manager,
            bool writePropertyName)
        {
            switch (node.NodeType)
            {
            case XmlNodeType.Element:
                if (this.IsArray(node) && XmlNodeConverter.AllSameName(node) && node.ChildNodes.Count > 0)
                {
                    this.SerializeGroupedNodes(writer, node, manager, false);
                    break;
                }

                manager.PushScope();
                foreach (IXmlNode attribute in node.Attributes)
                {
                    if (attribute.NamespaceUri == "http://www.w3.org/2000/xmlns/")
                    {
                        string prefix = attribute.LocalName != "xmlns"
                ? XmlConvert.DecodeName(attribute.LocalName)
                : string.Empty;
                        string uri = attribute.Value;
                        manager.AddNamespace(prefix, uri);
                    }
                }

                if (writePropertyName)
                {
                    writer.WritePropertyName(this.GetPropertyName(node, manager));
                }
                if (!this.ValueAttributes(node.Attributes) && node.ChildNodes.Count == 1 &&
                    node.ChildNodes[0].NodeType == XmlNodeType.Text)
                {
                    writer.WriteValue(node.ChildNodes[0].Value);
                }
                else if (node.ChildNodes.Count == 0 && node.Attributes.Count == 0)
                {
                    if (((IXmlElement)node).IsEmpty)
                    {
                        writer.WriteNull();
                    }
                    else
                    {
                        writer.WriteValue(string.Empty);
                    }
                }
                else
                {
                    writer.WriteStartObject();
                    for (int index = 0; index < node.Attributes.Count; ++index)
                    {
                        this.SerializeNode(writer, node.Attributes[index], manager, true);
                    }
                    this.SerializeGroupedNodes(writer, node, manager, true);
                    writer.WriteEndObject();
                }

                manager.PopScope();
                break;

            case XmlNodeType.Attribute:
            case XmlNodeType.Text:
            case XmlNodeType.CDATA:
            case XmlNodeType.ProcessingInstruction:
            case XmlNodeType.Whitespace:
            case XmlNodeType.SignificantWhitespace:
                if (node.NamespaceUri == "http://www.w3.org/2000/xmlns/" &&
                    node.Value == "http://james.newtonking.com/projects/json" ||
                    node.NamespaceUri == "http://james.newtonking.com/projects/json" && node.LocalName == "Array")
                {
                    break;
                }
                if (writePropertyName)
                {
                    writer.WritePropertyName(this.GetPropertyName(node, manager));
                }
                writer.WriteValue(node.Value);
                break;

            case XmlNodeType.Comment:
                if (!writePropertyName)
                {
                    break;
                }
                writer.WriteComment(node.Value);
                break;

            case XmlNodeType.Document:
            case XmlNodeType.DocumentFragment:
                this.SerializeGroupedNodes(writer, node, manager, writePropertyName);
                break;

            case XmlNodeType.DocumentType:
                IXmlDocumentType xmlDocumentType = (IXmlDocumentType)node;
                writer.WritePropertyName(this.GetPropertyName(node, manager));
                writer.WriteStartObject();
                if (!string.IsNullOrEmpty(xmlDocumentType.Name))
                {
                    writer.WritePropertyName("@name");
                    writer.WriteValue(xmlDocumentType.Name);
                }

                if (!string.IsNullOrEmpty(xmlDocumentType.Public))
                {
                    writer.WritePropertyName("@public");
                    writer.WriteValue(xmlDocumentType.Public);
                }

                if (!string.IsNullOrEmpty(xmlDocumentType.System))
                {
                    writer.WritePropertyName("@system");
                    writer.WriteValue(xmlDocumentType.System);
                }

                if (!string.IsNullOrEmpty(xmlDocumentType.InternalSubset))
                {
                    writer.WritePropertyName("@internalSubset");
                    writer.WriteValue(xmlDocumentType.InternalSubset);
                }

                writer.WriteEndObject();
                break;

            case XmlNodeType.XmlDeclaration:
                IXmlDeclaration xmlDeclaration = (IXmlDeclaration)node;
                writer.WritePropertyName(this.GetPropertyName(node, manager));
                writer.WriteStartObject();
                if (!string.IsNullOrEmpty(xmlDeclaration.Version))
                {
                    writer.WritePropertyName("@version");
                    writer.WriteValue(xmlDeclaration.Version);
                }

                if (!string.IsNullOrEmpty(xmlDeclaration.Encoding))
                {
                    writer.WritePropertyName("@encoding");
                    writer.WriteValue(xmlDeclaration.Encoding);
                }

                if (!string.IsNullOrEmpty(xmlDeclaration.Standalone))
                {
                    writer.WritePropertyName("@standalone");
                    writer.WriteValue(xmlDeclaration.Standalone);
                }

                writer.WriteEndObject();
                break;

            default:
                throw new JsonSerializationException("Unexpected XmlNodeType when serializing nodes: " +
                                                     (object)node.NodeType);
            }
        }