Exemplo n.º 1
0
        public XmlFile(System.IO.Stream fileStream)
        {
            // Test for XmlType
            try
            {
                doc = new XmlDocument();
                doc.Load(fileStream);
                type = XMLType.Text;
                return;
            }
            catch { doc = null; }
            finally
            {
                fileStream.Position = 0;
            }
            XmlBinaryReader r = new XmlBinaryReader(EndianBitConverter.Little, fileStream);
            byte headerByte = r.ReadByte();
            if (headerByte == 0x00)
            {
                type = XMLType.BXMLBig;
            }
            else if (headerByte == 0x01)
            {
                type = XMLType.BXMLLittle;
            }
            else
            {
                type = XMLType.BinXML;
            }
            r.BaseStream.Position = 0;
            r = null;

            // Create a text XML file
            if (type == XMLType.BinXML)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Little, fileStream))
                {
                    // Section 1
                    reader.ReadByte(); // Unique Byte
                    reader.ReadBytes(3); // Same Magic
                    reader.ReadInt32(); // File Length/Size

                    // Section 2
                    reader.ReadByte(); // Unique Byte
                    reader.ReadBytes(3); // Same Magic
                    reader.ReadInt32(); // Section 3 and 4 Total Length/Size

                    // Section 3 and 4
                    xmlStrings = new BinaryXmlString(reader);

                    // Section 5
                    reader.ReadInt32();
                    xmlElements = new BinaryXmlElement[reader.ReadInt32() / 24];
                    for (int i = 0; i < xmlElements.Length; i++)
                    {
                        xmlElements[i].elementNameID = reader.ReadInt32();
                        xmlElements[i].elementValueID = reader.ReadInt32();
                        xmlElements[i].attributeCount = reader.ReadInt32();
                        xmlElements[i].attributeStartID = reader.ReadInt32();
                        xmlElements[i].childElementCount = reader.ReadInt32();
                        xmlElements[i].childElementStartID = reader.ReadInt32();
                    }

                    // Section 6
                    reader.ReadInt32();
                    xmlAttributes = new BinaryXmlAttribute[reader.ReadInt32() / 8];
                    for (int i = 0; i < xmlAttributes.Length; i++)
                    {
                        xmlAttributes[i].nameID = reader.ReadInt32();
                        xmlAttributes[i].valueID = reader.ReadInt32();
                    }

                    // Build XML
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(xmlElements[0].CreateElement(doc, this));
                }
            }
            else if (type == XMLType.BXMLBig)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Big, fileStream))
                {
                    reader.ReadBytes(5);
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(reader.ReadBxmlElement(doc));
                }
            }
            else if (type == XMLType.BXMLLittle)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Little, fileStream))
                {
                    reader.ReadBytes(5);
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(reader.ReadBxmlElement(doc));
                }
            }
        }
Exemplo n.º 2
0
        public XmlFile(System.IO.Stream fileStream)
        {
            // Test for XmlType
            try
            {
                doc = new XmlDocument();
                doc.Load(fileStream);
                type = XMLType.Text;
                foreach (XmlNode child in doc.ChildNodes)
                {
                    if (child.NodeType == XmlNodeType.Comment)
                    {
                        try
                        {
                            type = (XMLType)Enum.Parse(typeof(XMLType), child.Value);
                            break;
                        }
                        catch
                        {
                        }
                    }
                }
                return;
            }
            catch { doc = null; }
            finally
            {
                fileStream.Position = 0;
            }
            XmlBinaryReader r          = new XmlBinaryReader(EndianBitConverter.Little, fileStream);
            byte            headerByte = r.ReadByte();

            if (headerByte == 0x00)
            {
                type = XMLType.BXMLBig;
            }
            else if (headerByte == 0x01)
            {
                type = XMLType.BXMLLittle;
            }
            else
            {
                type = XMLType.BinXML;
            }
            r.BaseStream.Position = 0;
            r = null;

            // Create a text XML file
            if (type == XMLType.BinXML)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Little, fileStream))
                {
                    // Section 1
                    reader.ReadByte();   // Unique Byte
                    reader.ReadBytes(3); // Same Magic
                    reader.ReadInt32();  // File Length/Size

                    // Section 2
                    reader.ReadByte();   // Unique Byte
                    reader.ReadBytes(3); // Same Magic
                    reader.ReadInt32();  // Section 3 and 4 Total Length/Size

                    // Section 3 and 4
                    xmlStrings = new BinaryXmlString(reader);

                    // Section 5
                    reader.ReadInt32();
                    xmlElements = new BinaryXmlElement[reader.ReadInt32() / 24];
                    for (int i = 0; i < xmlElements.Length; i++)
                    {
                        xmlElements[i].elementNameID       = reader.ReadInt32();
                        xmlElements[i].elementValueID      = reader.ReadInt32();
                        xmlElements[i].attributeCount      = reader.ReadInt32();
                        xmlElements[i].attributeStartID    = reader.ReadInt32();
                        xmlElements[i].childElementCount   = reader.ReadInt32();
                        xmlElements[i].childElementStartID = reader.ReadInt32();
                    }

                    // Section 6
                    reader.ReadInt32();
                    xmlAttributes = new BinaryXmlAttribute[reader.ReadInt32() / 8];
                    for (int i = 0; i < xmlAttributes.Length; i++)
                    {
                        xmlAttributes[i].nameID  = reader.ReadInt32();
                        xmlAttributes[i].valueID = reader.ReadInt32();
                    }

                    // Build XML
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(doc.CreateComment(type.ToString()));
                    doc.AppendChild(xmlElements[0].CreateElement(doc, this));
                }
            }
            else if (type == XMLType.BXMLBig)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Big, fileStream))
                {
                    reader.ReadBytes(5);
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(doc.CreateComment(type.ToString()));
                    doc.AppendChild(reader.ReadBxmlElement(doc));
                }
            }
            else if (type == XMLType.BXMLLittle)
            {
                using (XmlBinaryReader reader = new XmlBinaryReader(EndianBitConverter.Little, fileStream))
                {
                    reader.ReadBytes(5);
                    doc = new XmlDocument();
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"));
                    doc.AppendChild(doc.CreateComment(type.ToString()));
                    doc.AppendChild(reader.ReadBxmlElement(doc));
                }
            }
        }