예제 #1
0
        public XmlNode Process(XmlSchema schema, string rootNode)
        {
            XmlNode root = new XmlNode
            {
                Name   = "<root>",
                IsRoot = true
            };

            SchemaSet = new XmlSchemaSet();
            SchemaSet.Add(schema);

            SchemaSet.Compile();

            Traverse(root, schema.Items);

            return(root.ChildNode(rootNode));
        }
예제 #2
0
        private void WriteElementOrAttribute(XmlSchemaObject.XmlNode schemaNode, string elementName, XmlWriter attributeWriter, XmlWriter elementWriter)
        {
            if (schemaNode == null)
            {
                return;
            }

            XmlSchemaObject.XmlNode childNode = schemaNode.ChildNode(elementName);

            if (childNode == null)
            {
                return;
            }

            if (childNode.IsAttribute)
            {
                WriteValue(childNode, attributeWriter);
            }
            else
            {
                WriteValue(childNode, elementWriter);
            }
        }
예제 #3
0
        private Stream WriteObject(XmlSchemaObject.XmlNode schemaNode)
        {
            VirtualStream elementStream = new VirtualStream();


            XmlWriter elementWriter = XmlWriter.Create(elementStream, new XmlWriterSettings {
                ConformanceLevel = ConformanceLevel.Fragment
            });

            VirtualStream objectStream = new VirtualStream();
            XmlWriter     objectWriter = XmlWriter.Create(objectStream, new XmlWriterSettings {
                ConformanceLevel = ConformanceLevel.Fragment, CloseOutput = false
            });

            string elementName = String.Empty;

            if (schemaNode != null)
            {
                objectWriter.WriteStartElement(Settings.Prefix, schemaNode.Name, schemaNode.Namespace);
            }


            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndObject)
                {
                    if (schemaNode == null)
                    {
                        return(null);
                    }

                    break;
                }

                switch (reader.TokenType)
                {
                case JsonToken.StartObject:
                    if (elementName != String.Empty)
                    {
                        XmlSchemaObject.XmlNode objectNode = schemaNode == null ? null : schemaNode.ChildNode(elementName);

                        Stream retObjectStream = WriteObject(objectNode);

                        WriteElementFragment(elementWriter, retObjectStream);
                    }

                    break;

                case JsonToken.Date:
                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Boolean:
                case JsonToken.Bytes:

                    WriteElementOrAttribute(schemaNode, elementName, objectWriter, elementWriter);

                    break;


                case JsonToken.Null:

                    // empty element. do nothing
                    break;

                case JsonToken.PropertyName:

                    elementName = (string)reader.Value;


                    break;

                // case JsonToken.EndArray:
                //   return;
                case JsonToken.StartArray:
                    XmlSchemaObject.XmlNode arrayNode = schemaNode == null ? null : schemaNode.ChildNode(elementName);

                    var innerArray = WriteArray(arrayNode);

                    WriteElementFragment(elementWriter, innerArray);


                    break;
                    //  default:
                }
            }

            WriteElementFragment(objectWriter, elementStream, elementWriter);

            objectWriter.WriteEndElement();
            objectWriter.Flush();
            objectWriter.Close();

            objectStream.Position = 0;

            return(objectStream);
        }
예제 #4
0
        private Stream WriteArray(XmlSchemaObject.XmlNode schemaNode)
        {
            VirtualStream arrayStream = new VirtualStream();
            XmlWriter     arrayWriter = XmlWriter.Create(arrayStream, new XmlWriterSettings {
                ConformanceLevel = ConformanceLevel.Fragment, CloseOutput = false
            });

            string elementName = String.Empty;

            while (reader.Read())
            {
                if (reader.TokenType == JsonToken.EndArray)
                {
                    if (schemaNode == null)
                    {
                        return(null);
                    }

                    break;
                }

                switch (reader.TokenType)
                {
                case JsonToken.StartObject:
                    XmlSchemaObject.XmlNode objectNode = schemaNode == null ?null: schemaNode.CurrentOrDefaultChild(elementName);

                    var objectStream = WriteObject(objectNode);

                    WriteElementFragment(arrayWriter, objectStream);


                    break;

                case JsonToken.Date:
                case JsonToken.String:
                case JsonToken.Integer:
                case JsonToken.Float:
                case JsonToken.Boolean:
                case JsonToken.Bytes:

                    if (schemaNode != null)
                    {
                        string prefix = Settings.Prefix;
                        string ns     = schemaNode.Namespace;

                        if (String.IsNullOrEmpty(schemaNode.Namespace))
                        {
                            prefix = null;
                            ns     = null;
                        }

                        arrayWriter.WriteStartElement(prefix, elementName == String.Empty ? schemaNode.Name : elementName, ns);
                        arrayWriter.WriteValue(reader.Value);
                        arrayWriter.WriteEndElement();
                    }

                    break;


                case JsonToken.Null:

                    // empty element. do nothing
                    break;

                case JsonToken.PropertyName:


                    elementName = (string)reader.Value;


                    break;

                case JsonToken.StartArray:
                    XmlSchemaObject.XmlNode arrayNode = schemaNode == null ? null : schemaNode.ChildNode(elementName);

                    Stream childArray = WriteArray(arrayNode);

                    WriteElementFragment(arrayWriter, childArray);

                    break;
                    //  default:
                }
            }

            arrayWriter.Flush();
            arrayWriter.Close();
            arrayStream.Position = 0;

            return(arrayStream);
        }