1

I have XML content that I am retrieving from a service and I want to write it into a text file. I am getting this XML content in a string variable.

How can I read this and write in text file? Please help. I am getting data successfully using this code:

string results = "";
using (WebClient Web = new WebClient())
{
    results = Web.DownloadString(WebString.ToString());
}

I have tried some links, but they are not helping

1 Answer 1

1

If you want to save it in a file, don't use DownloadString to start with - just use WebClient.DownloadFile.

If you really want to fetch it in memory and then save it, you can save it with:

File.WriteAllText(filename, results);

Note that this code doesn't depend on it being XML at all... nothing you're asking is XML-specific.

Sign up to request clarification or add additional context in comments.

4 Comments

But In case of Web.DownloadString() how to read it. just for knowledge.
no overload DownloadFile takes one argument error comes when I tried this
@Sandy: Well no, you need to specify the URL to fetch, and the file to save it to. Did you read the documentation? Which part was problematic? As for DownloadString - I don't know what you mean by "how to read it"... you've fetched it as a string...
yes I have fetched it as string and it is present with me. now I want to write this string in text file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.