/// <summary>
        /// Reads information from WMAppManifest.xml.
        /// </summary>
        /// <returns></returns>
        public bool Read()
        {
            if (_fileName == null)
                throw new Exception("File name is empty");
            GenerateTempFolder();
            UpdateView();
            //Cleanup();

            error = XapReadError.NotRead;

            string TempFileName = _fileName;
            bool deleteFile = false;
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            XDocument reader = null;

            StreamResourceInfo xapStream = null;
            try
            {
                if (TempFileName.Contains("\\"))
                {
                    string shortname = _fileName.Substring(_fileName.LastIndexOf("\\") + 1).Replace("%20", " ");
                    if (shortname.Contains(" "))
                    {
                        bool res = Functions.CopyFile(_fileName, "\\Temp\\temp.xap");
                        if (res == false)
                        {
                            error = XapReadError.CouldNotCopyFile;
                            throw new Exception("Could not copy file");
                        }
                        TempFileName = "\\Temp\\temp.xap";
                        deleteFile = true;
                    }
                }
                var uri = new Uri(TempFileName, UriKind.Relative);
                xapStream = Application.GetResourceStream(uri);
                if (xapStream == null)
                {
                    error = XapReadError.OpenError;
                    throw new Exception("Couldn't read XAP");
                }
                _fileSize = xapStream.Stream.Length;

                // Input WMAppManifest not always has valid encoding. Let's try different ones to find the best.
                var stream = Application.GetResourceStream(xapStream, new Uri("WMAppManifest.xml", UriKind.Relative)).Stream;
                if (stream == null)
                {
                    error = XapReadError.CouldNotOpenWMAppManifest;
                    throw new Exception("Couldn't open WMAppManifest");
                }
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Close();
                stream = null;
                var encodings = new System.Text.Encoding[3] { Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode };
                bool correctEncodingFound = false;
                for (int i = 0; i < encodings.Length; ++i)
                {
                    try
                    {
                        Encoding enc = encodings[i];
                        string text = enc.GetString(bytes, 0, bytes.Length);
                        text = text.Replace("utf-16", "utf-8");
                        byte[] newBytes = Encoding.UTF8.GetBytes(text);

                        var memStream = new MemoryStream(newBytes);
                        reader = XDocument.Load(memStream);
                        memStream.Close();
                        memStream = null;
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                    correctEncodingFound = true;
                    break;
                }
                if (!correctEncodingFound)
                {
                    error = XapReadError.InvalidEncoding;
                    throw new Exception("Invalid WMAppManifest.xml encoding");
                }
            }
            catch (Exception ex)
            {
                if (error == XapReadError.NotRead)
                    error = XapReadError.OpenError;
                if (deleteFile == true)
                    Functions.RemoveFile(TempFileName);
                UpdateView();
                return false;

            }
            _isSigned = false;
            try
            {
                var rs = Application.GetResourceStream(xapStream, new Uri("WMAppPRHeader.xml", UriKind.Relative));
                if (rs != null)
                {
                    _isSigned = true;
                    rs.Stream.Close();
                }
            }
            catch (Exception ex)
            {
                _isSigned = false;
            }
            var nodes = reader.Descendants().Descendants<XElement>();
            bool AppNodeFound = false;
            bool ProductIdFound = false;
            foreach (var node in nodes)
            {
                string uri = node.BaseUri;
                if (node.Name.LocalName == "App")
                {
                    AppNodeFound = true;

                    var attrs = node.Attributes();
                    foreach (var attr in attrs)
                    {
                        switch (attr.Name.LocalName)
                        {
                            case "ProductID":
                                _productId = attr.Value;
                                ProductIdFound = true;
                                break;
                            case "Title":
                                _title = attr.Value;
                                break;
                            case "Version":
                                _version = attr.Value;
                                break;
                            case "Author":
                                _author = attr.Value;
                                break;
                            case "Description":
                                _description = attr.Value;
                                break;
                            case "Publisher":
                                _publisher = attr.Value;
                                break;
                            case "Genre":
                                _genre = attr.Value;
                                break;
                        }
                    }

                    _title = ExtractResourceString(xapStream, _title);
                    _version = ExtractResourceString(xapStream, _version);
                    _author = ExtractResourceString(xapStream, _author);
                    _description = ExtractResourceString(xapStream, _description);
                    _publisher = ExtractResourceString(xapStream, _publisher);

                    var elems = node.Elements();
                    foreach (var elem in elems)
                    {
                        switch (elem.Name.LocalName)
                        {
                            case "IconPath":
                                _applicationIcon = elem.Value;
                                if (ExtractFile(xapStream, isf, _applicationIcon) == true)
                                {
                                    string shortFileName = ShortenFileName(_applicationIcon);
                                    _applicationIcon = shortFileName;
                                }
                                break;
                            case "Capabilities":
                                var caps = elem.Elements();
                                foreach (var cap in caps)
                                {
                                    Capabilities.Add(cap.Attribute("Name").Value);
                                }
                                break;
                        }
                    }
                }
            }
            xapStream.Stream.Close();
            xapStream.Stream.Dispose();
            xapStream = null;
            reader = null;

            if (!AppNodeFound)
                error = XapReadError.NoAppElement;
            else if (!ProductIdFound)
                error = XapReadError.NoProductId;
            if (error == XapReadError.NotRead)
                error = XapReadError.Success;

            if (deleteFile == true)
            {
                Functions.RemoveFile(TempFileName);
            }
            UpdateView();
            return true;
        }
        /// <summary>
        /// Reads information from WMAppManifest.xml.
        /// </summary>
        /// <returns></returns>
        public bool Read()
        {
            if (_fileName == null)
            {
                throw new Exception("File name is empty");
            }
            UpdateView();
            //Cleanup();

            error = XapReadError.NotRead;

            string TempFileName     = _fileName;
            bool   deleteFile       = false;
            IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

            XDocument reader = null;

            StreamResourceInfo xapStream = null;

            try
            {
                if (TempFileName.Contains("\\"))
                {
                    string shortname = _fileName.Substring(_fileName.LastIndexOf("\\") + 1).Replace("%20", " ");
                    if (shortname.Contains(" "))
                    {
                        bool res = Functions.CopyFile(_fileName, "\\Temp\\temp.xap");
                        if (res == false)
                        {
                            error = XapReadError.CouldNotCopyFile;
                            throw new Exception("Could not copy file");
                        }
                        TempFileName = "\\Temp\\temp.xap";
                        deleteFile   = true;
                    }
                }
                var uri = new Uri(TempFileName, UriKind.Relative);
                xapStream = Application.GetResourceStream(uri);
                if (xapStream == null)
                {
                    error = XapReadError.OpenError;
                    throw new Exception("Couldn't read XAP");
                }
                _fileSize = xapStream.Stream.Length;

                // Input WMAppManifest not always has valid encoding. Let's try different ones to find the best.
                var stream = Application.GetResourceStream(xapStream, new Uri("WMAppManifest.xml", UriKind.Relative)).Stream;
                if (stream == null)
                {
                    error = XapReadError.CouldNotOpenWMAppManifest;
                    throw new Exception("Couldn't open WMAppManifest");
                }
                byte[] bytes = new byte[stream.Length];
                stream.Read(bytes, 0, bytes.Length);
                stream.Close();
                stream = null;
                var encodings = new System.Text.Encoding[3] {
                    Encoding.UTF8, Encoding.Unicode, Encoding.BigEndianUnicode
                };
                bool correctEncodingFound = false;
                for (int i = 0; i < encodings.Length; ++i)
                {
                    try
                    {
                        Encoding enc  = encodings[i];
                        string   text = enc.GetString(bytes, 0, bytes.Length);
                        text = text.Replace("utf-16", "utf-8");
                        byte[] newBytes = Encoding.UTF8.GetBytes(text);

                        var memStream = new MemoryStream(newBytes);
                        reader = XDocument.Load(memStream);
                        memStream.Close();
                        memStream = null;
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                    correctEncodingFound = true;
                    break;
                }
                if (!correctEncodingFound)
                {
                    error = XapReadError.InvalidEncoding;
                    throw new Exception("Invalid WMAppManifest.xml encoding");
                }
            }
            catch (Exception ex)
            {
                if (error == XapReadError.NotRead)
                {
                    error = XapReadError.OpenError;
                }
                if (deleteFile == true)
                {
                    Functions.RemoveFile(TempFileName);
                }
                UpdateView();
                return(false);
            }
            _isSigned = false;
            try
            {
                var rs = Application.GetResourceStream(xapStream, new Uri("WMAppPRHeader.xml", UriKind.Relative));
                if (rs != null)
                {
                    _isSigned = true;
                    rs.Stream.Close();
                }
            }
            catch (Exception ex)
            {
                _isSigned = false;
            }
            var  nodes          = reader.Descendants().Descendants <XElement>();
            bool AppNodeFound   = false;
            bool ProductIdFound = false;

            foreach (var node in nodes)
            {
                string uri = node.BaseUri;
                if (node.Name.LocalName == "App")
                {
                    AppNodeFound = true;

                    var attrs = node.Attributes();
                    foreach (var attr in attrs)
                    {
                        switch (attr.Name.LocalName)
                        {
                        case "ProductID":
                            _productId     = attr.Value;
                            ProductIdFound = true;
                            break;

                        case "Title":
                            _title = attr.Value;
                            break;

                        case "Version":
                            _version = attr.Value;
                            break;

                        case "Author":
                            _author = attr.Value;
                            break;

                        case "Description":
                            _description = attr.Value;
                            break;

                        case "Publisher":
                            _publisher = attr.Value;
                            break;

                        case "Genre":
                            _genre = attr.Value;
                            break;
                        }
                    }

                    _title       = ExtractResourceString(xapStream, _title);
                    _version     = ExtractResourceString(xapStream, _version);
                    _author      = ExtractResourceString(xapStream, _author);
                    _description = ExtractResourceString(xapStream, _description);
                    _publisher   = ExtractResourceString(xapStream, _publisher);

                    var elems = node.Elements();
                    foreach (var elem in elems)
                    {
                        switch (elem.Name.LocalName)
                        {
                        case "IconPath":
                            _applicationIcon = elem.Value;
                            if (ExtractFile(xapStream, isf, _applicationIcon) == true)
                            {
                                string shortFileName = ShortenFileName(_applicationIcon);
                                _applicationIcon = shortFileName;
                            }
                            break;

                        case "Capabilities":
                            var caps = elem.Elements();
                            foreach (var cap in caps)
                            {
                                Capabilities.Add(cap.Attribute("Name").Value);
                            }
                            break;
                        }
                    }
                }
            }
            xapStream.Stream.Close();
            xapStream.Stream.Dispose();
            xapStream = null;
            reader    = null;


            if (!AppNodeFound)
            {
                error = XapReadError.NoAppElement;
            }
            else if (!ProductIdFound)
            {
                error = XapReadError.NoProductId;
            }
            if (error == XapReadError.NotRead)
            {
                error = XapReadError.Success;
            }

            if (deleteFile == true)
            {
                Functions.RemoveFile(TempFileName);
            }
            UpdateView();
            return(true);
        }