Answer the question
In order to leave comments, you need to log in
I get xml by url, how can I save the content into a new xml file, so that the structure is not broken?
When I do this, the structure is broken
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))
{
//это для считывания xml файла
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))
{
//это для считывания xml файла
using (var writer = new XmlTextWriter("weather1.xml", Encoding.UTF8))
{
writer.WriteString(reader.ReadString());
}
}
}
}
Answer the question
In order to leave comments, you need to log in
According to the first option - what does "the structure is broken" mean? It should not be broken during character-by-character copying. However, there may be problems with encodings. The required encoding must be determined either by ContentType - or by the xml declaration.
According to the second option - of course, this should not work. Read the documentation for what ReadString does.
--
Now how to save xml. The first option is just byte by byte. Use stream directly without wrapping it in a StreamReader - and use a FileStream for writing.
The second option is to load the stream into an XmlDocument or XDocument of your choice, and then save it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question