Пример #1
0
        public static async Task RoundtripSyncAsyncCheckAndMatches_StreamReader(bool document, LoadOptions loadOptions, SaveOptions saveOptions)
        {
            // Roundtrip XML using synchronous and StreamReader/Writer
            CheckSyncAsyncStream syncInput        = new CheckSyncAsyncStream(async: false, FilePathUtil.getStream(GetTestFileName()));
            MemoryStream         syncOutputMemory = new MemoryStream();
            CheckSyncAsyncStream syncOutput       = new CheckSyncAsyncStream(async: false, syncOutputMemory);

            using (StreamReader syncReader = new StreamReader(syncInput))
                using (StreamWriter syncWriter = new StreamWriter(syncOutput))
                {
                    if (document)
                    {
                        XDocument syncDoc = XDocument.Load(syncReader, loadOptions);
                        syncDoc.Save(syncWriter, saveOptions);
                    }
                    else
                    {
                        XElement syncElement = XElement.Load(syncReader, loadOptions);
                        syncElement.Save(syncWriter, saveOptions);
                    }
                }

            // Roundtrip XML using asynchronous and StreamReader/Writer
            CheckSyncAsyncStream asyncInput        = new CheckSyncAsyncStream(async: true, FilePathUtil.getStream(GetTestFileName()));
            MemoryStream         asyncOutputMemory = new MemoryStream();
            CheckSyncAsyncStream asyncOutput       = new CheckSyncAsyncStream(async: true, asyncOutputMemory);

            using (StreamReader asyncReader = new StreamReader(asyncInput))
                await using (StreamWriter asyncWriter = new StreamWriter(asyncOutput))
                {
                    if (document)
                    {
                        XDocument asyncDoc = await XDocument.LoadAsync(asyncReader, loadOptions, CancellationToken.None);

                        await asyncDoc.SaveAsync(asyncWriter, saveOptions, CancellationToken.None);
                    }
                    else
                    {
                        XElement asyncElement = await XElement.LoadAsync(asyncReader, loadOptions, CancellationToken.None);

                        await asyncElement.SaveAsync(asyncWriter, saveOptions, CancellationToken.None);
                    }
                }

            // Compare to make sure the synchronous and asynchronous results are the same
            Assert.Equal(syncOutputMemory.ToArray(), asyncOutputMemory.ToArray());
        }
Пример #2
0
        public static async Task RoundtripSyncAsyncCheckAndMatches_XmlReader(bool document, LoadOptions loadOptions, SaveOptions saveOptions)
        {
            // Create reader and writer settings
            var readerSettings = new XmlReaderSettings();
            var writerSettings = new XmlWriterSettings();

            if ((saveOptions & SaveOptions.OmitDuplicateNamespaces) != 0)
            {
                writerSettings.NamespaceHandling = NamespaceHandling.OmitDuplicates;
            }
            if ((saveOptions & SaveOptions.DisableFormatting) != 0)
            {
                writerSettings.Indent          = false;
                writerSettings.NewLineHandling = NewLineHandling.None;
            }

            // Roundtrip XML using synchronous and XmlReader/Writer
            CheckSyncAsyncStream syncInput        = new CheckSyncAsyncStream(async: false, FilePathUtil.getStream(GetTestFileName()));
            MemoryStream         syncOutputMemory = new MemoryStream();
            CheckSyncAsyncStream syncOutput       = new CheckSyncAsyncStream(async: false, syncOutputMemory);

            using (XmlReader syncReader = XmlReader.Create(syncInput, readerSettings))
                using (XmlWriter syncWriter = XmlWriter.Create(syncOutput, writerSettings))
                {
                    if (document)
                    {
                        XDocument syncDoc = XDocument.Load(syncReader, loadOptions);

                        syncDoc.Save(syncWriter);
                    }
                    else
                    {
                        XElement syncElement = XElement.Load(syncReader, loadOptions);
                        syncElement.Save(syncWriter);
                    }
                }

            // Roundtrip XML using asynchronous and XmlReader/Writer
            readerSettings.Async = true;
            writerSettings.Async = true;
            CheckSyncAsyncStream asyncInput        = new CheckSyncAsyncStream(async: true, FilePathUtil.getStream(GetTestFileName()));
            MemoryStream         asyncOutputMemory = new MemoryStream();
            CheckSyncAsyncStream asyncOutput       = new CheckSyncAsyncStream(async: true, asyncOutputMemory);

            using (XmlReader asyncReader = XmlReader.Create(asyncInput, readerSettings))
                await using (XmlWriter asyncWriter = XmlWriter.Create(asyncOutput, writerSettings))
                {
                    if (document)
                    {
                        XDocument asyncDoc = await XDocument.LoadAsync(asyncReader, loadOptions, CancellationToken.None);

                        await asyncDoc.SaveAsync(asyncWriter, CancellationToken.None);
                    }
                    else
                    {
                        XElement asyncElement = await XElement.LoadAsync(asyncReader, loadOptions, CancellationToken.None);

                        await asyncElement.SaveAsync(asyncWriter, CancellationToken.None);
                    }
                }

            // Compare to make sure the synchronous and asynchronous results are the same
            Assert.Equal(syncOutputMemory.ToArray(), asyncOutputMemory.ToArray());
        }