示例#1
0
        /// <summary>
        /// The preferred way to save - this should be a <see cref="System.Xml.XmlDocument" /> straight from CCP.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="xdoc">The xml to save.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static async Task SaveAsync(string filename, IXPathNavigable xdoc)
        {
            xdoc.ThrowIfNull(nameof(xdoc));

            EveMonClient.EnsureCacheDirInit();

            XmlDocument xmlDoc        = (XmlDocument)xdoc;
            XmlNode     characterNode = xmlDoc.SelectSingleNode("//name");

            filename = characterNode?.InnerText ?? filename;

            // Writes in the target file
            string fileName = Path.Combine(EveMonClient.EVEMonXmlCacheDir, $"{filename}.xml");
            string content  = Util.GetXmlStringRepresentation(xdoc);
            await FileHelper.OverwriteOrWarnTheUserAsync(fileName,
                                                         async fs =>
            {
                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
                {
                    await writer.WriteAsync(content);
                    await writer.FlushAsync();
                    await fs.FlushAsync();
                }
                return(true);
            });
        }
示例#2
0
        /// <summary>
        /// The preferred way to save - this should be a <see cref="System.Xml.XmlDocument" /> straight from CCP.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="xdoc">The xml to save.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static async Task SaveAsync(string filename, IXPathNavigable xdoc)
        {
            xdoc.ThrowIfNull(nameof(xdoc));

            EveMonClient.EnsureCacheDirInit();

            XmlDocument xmlDoc = (XmlDocument)xdoc;
            XmlNode characterNode = xmlDoc.SelectSingleNode("//name");
            filename = characterNode?.InnerText ?? filename;

            // Writes in the target file
            string fileName = Path.Combine(EveMonClient.EVEMonXmlCacheDir, $"{filename}.xml");
            string content = Util.GetXmlStringRepresentation(xdoc);
            await FileHelper.OverwriteOrWarnTheUserAsync(fileName,
                async fs =>
                {
                    using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
                    {
                        await writer.WriteAsync(content);
                        await writer.FlushAsync();
                        await fs.FlushAsync();
                    }
                    return true;
                });
        }
示例#3
0
        /// <summary>
        /// Saves the document to the disk.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="xmlDocument">The XML document.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">filename or xmlDocument</exception>
        public static async Task SaveDocumentAsync(string filename, IXPathNavigable xmlDocument)
        {
            filename.ThrowIfNull(nameof(filename));

            xmlDocument.ThrowIfNull(nameof(xmlDocument));

            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.DefaultExt = "xml";
                sfd.Filter     = @"XML (*.xml)|*.xml";
                sfd.FileName   = filename;

                if (sfd.ShowDialog() != DialogResult.OK)
                {
                    return;
                }

                try
                {
                    string content = Util.GetXmlStringRepresentation(xmlDocument);

                    // Moves to the final file
                    await FileHelper.OverwriteOrWarnTheUserAsync(
                        sfd.FileName,
                        async fs =>
                    {
                        using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
                        {
                            await writer.WriteAsync(content);
                            await writer.FlushAsync();
                            await fs.FlushAsync();
                        }
                        return(true);
                    });
                }
                catch (IOException err)
                {
                    ExceptionHandler.LogException(err, true);
                    MessageBox.Show($"There was an error writing out the file:\n\n{err.Message}",
                                    @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (XmlException err)
                {
                    ExceptionHandler.LogException(err, true);
                    MessageBox.Show($"There was an error while converting to XML format.\r\nThe message was:\r\n{err.Message}",
                                    @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
示例#4
0
        /// <summary>
        /// Gets a nicely formatted string representation of a XML document.
        /// </summary>
        /// <param name="doc">The document.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static string GetXmlStringRepresentation(IXPathNavigable doc)
        {
            doc.ThrowIfNull(nameof(doc));

            // Creates the settings for the text writer
            XmlWriterSettings settings = new XmlWriterSettings {
                Indent = true, NewLineHandling = NewLineHandling.Replace
            };

            // Writes to a string builder
            StringBuilder xmlBuilder = new StringBuilder();

            using (XmlWriter xmlWriter = XmlWriter.Create(xmlBuilder, settings))
            {
                XmlDocument xmlDoc = (XmlDocument)doc;
                xmlDoc.WriteContentTo(xmlWriter);
                xmlWriter.Flush();
            }
            return(xmlBuilder.ToString());
        }
示例#5
0
        /// <summary>
        /// Applies a XSLT to a <see cref="XmlDocument" /> and returns another <see cref="XmlDocument" />.
        /// </summary>
        /// <param name="doc">The source <see cref="XmlDocument" /></param>
        /// <param name="xslt">The XSLT to apply.</param>
        /// <returns>
        /// The transformed <see cref="XmlDocument" />.
        /// </returns>
        /// <exception cref="System.ArgumentNullException"></exception>
        public static IXPathNavigable Transform(IXPathNavigable doc, XslCompiledTransform xslt)
        {
            doc.ThrowIfNull(nameof(doc));

            xslt.ThrowIfNull(nameof(xslt));

            MemoryStream stream = GetMemoryStream();

            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
            {
                // Apply the XSL transform
                writer.Formatting = Formatting.Indented;
                xslt.Transform(doc, writer);
                writer.Flush();

                // Reads the XML document from the given stream.
                stream.Seek(0, SeekOrigin.Begin);
                XmlDocument outDoc = new XmlDocument();
                outDoc.Load(stream);
                return(outDoc);
            }
        }
示例#6
0
        /// <summary>
        /// Saves the document to the disk.
        /// </summary>
        /// <param name="filename">The filename.</param>
        /// <param name="xmlDocument">The XML document.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentNullException">filename or xmlDocument</exception>
        public static async Task SaveDocumentAsync(string filename, IXPathNavigable xmlDocument)
        {
            filename.ThrowIfNull(nameof(filename));

            xmlDocument.ThrowIfNull(nameof(xmlDocument));

            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.DefaultExt = "xml";
                sfd.Filter = @"XML (*.xml)|*.xml";
                sfd.FileName = filename;

                if (sfd.ShowDialog() != DialogResult.OK)
                    return;

                try
                {
                    string content = Util.GetXmlStringRepresentation(xmlDocument);

                    // Moves to the final file
                    await FileHelper.OverwriteOrWarnTheUserAsync(
                        sfd.FileName,
                        async fs =>
                            {
                                using (StreamWriter writer = new StreamWriter(fs, Encoding.UTF8))
                                {
                                    await writer.WriteAsync(content);
                                    await writer.FlushAsync();
                                    await fs.FlushAsync();
                                }
                                return true;
                            });
                }
                catch (IOException err)
                {
                    ExceptionHandler.LogException(err, true);
                    MessageBox.Show($"There was an error writing out the file:\n\n{err.Message}",
                                    @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (XmlException err)
                {
                    ExceptionHandler.LogException(err, true);
                    MessageBox.Show($"There was an error while converting to XML format.\r\nThe message was:\r\n{err.Message}",
                                    @"Save Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }