예제 #1
0
        // Reads all the strings available in the binary xml file
        // First Part is string data
        // Second part is position index of string data, not used by program
        public BinaryXmlString(XmlBinaryReader reader)
        {
            // Section 3
            reader.ReadInt32();
            int           len    = reader.ReadInt32();
            long          endPos = len + reader.BaseStream.Position;
            List <string> vals   = new List <string>();

            while (reader.BaseStream.Position < endPos)
            {
                string str = reader.ReadTerminatedString();
                //if (!string.IsNullOrEmpty(str))
                //{
                vals.Add(str);
                //}
            }
            values = vals.ToArray();
            //System.Windows.Forms.MessageBox.Show(values.Length.ToString());

            // Section 4
            reader.ReadInt32();
            int len2 = reader.ReadInt32();

            reader.Seek(len2, System.IO.SeekOrigin.Current);
        }
        // Reads all the strings available in the binary xml file
        // First Part is string data
        // Second part is position index of string data, not used by program
        public BinaryXmlString(XmlBinaryReader reader)
        {
            // Section 3
            reader.ReadInt32();
            int len = reader.ReadInt32();
            long endPos = len + reader.BaseStream.Position;
            List<string> vals = new List<string>();
            while (reader.BaseStream.Position < endPos)
            {
                string str = reader.ReadTerminatedString();
                //if (!string.IsNullOrEmpty(str))
                //{
                vals.Add(str);
                //}
            }
            values = vals.ToArray();
            //System.Windows.Forms.MessageBox.Show(values.Length.ToString());

            // Section 4
            reader.ReadInt32();
            int len2 = reader.ReadInt32();
            reader.Seek(len2, System.IO.SeekOrigin.Current);
        }
예제 #3
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));
                }
            }
        }