Получаю xml по url, как сохранить содержимое в новый xml файл, только чтобы структура не нарушилась? - C#
Формулировка задачи:
Когда делаю так, структура нарушается
Когда так, то ничего не записывается в файл
WebRequest request = WebRequest.Create(@"http://xml.weather.co.ua/1.2/forecast/27?dayf=5&userid=fdsf_com_ua&lang=ru");
using (var response = request.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream != null)
using (
var reader = new StreamReader(stream))
{
using (var writer = new StreamWriter("weather.xml", false))
{
writer.Write(reader.ReadToEnd());
}
}
}
}WebRequest request1 = WebRequest.Create(@"http://xml.weather.co.ua/1.2/forecast/27?dayf=5&userid=fdsf_com_ua&lang=ru");
using (var response = request1.GetResponse())
{
using (var stream = response.GetResponseStream())
{
if (stream != null)
using (
var reader = new XmlTextReader(stream))
{
using (var writer = new XmlTextWriter("weather1.xml", Encoding.UTF8))
{
writer.WriteString(reader.ReadString());
}
}
}
}Решение задачи: «Получаю xml по url, как сохранить содержимое в новый xml файл, только чтобы структура не нарушилась?»
textual
Листинг программы
private static void CopyStream(Stream source, Stream destination)
{
if (destination == null) throw new ArgumentNullException("destination");
if (!source.CanRead && !source.CanWrite) throw new ObjectDisposedException("source");
if (!destination.CanRead && !destination.CanWrite) throw new ObjectDisposedException("destination");
if (!source.CanRead) throw new NotSupportedException("Source stream doesn't support reading");
if (!destination.CanWrite) throw new NotSupportedException("Destination stream doesn't support writing");
byte[] buffer = new byte[16384];
int count;
while ((count = source.Read(buffer, 0, buffer.Length)) != 0)
{
destination.Write(buffer, 0, count);
}
}