0

I am writing a string to a xml document.

I now have several different strings and I want to write all of them to one xml file. Is there any way of doing this instead of having numerous single xml files?

3 Answers 3

2

Instead of writing strings out, use XDocument to construct the XML structure and Save to a file.

This will keep you from making mistakes in the format of the XML file itself, as XML is not simple text, but a structured file format.

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

3 Comments

Im fairly new to vb.net and i needed to get the contents of my dictionary to xml format. I thought the easiest way to do this was to use a string builder that would write the contents in a basic xml output.
@ASmith - Superficially, perhaps. But by doing so you can end up with invalid XML easily and find it difficult to find where the error is. A strongly typed way safeguards you from such errors.
@ASmith - It doesn't matter whether you are newbie or not. You must have to use DOM, XDocument or SAX parsert to write/read XML document.
0

Use System.IO.File.AppendAllText instead of System.IO.File.WriteAllText

1 Comment

Which will result in an invalid XML file.
0

Use file.AppendText to add to a file.

4 Comments

Which will result in an invalid XML file.
@Oded, yes, but as far as I can see the question did not state that the file should be valid xml.
It is implied. If you write to a file, you expect to read from it. And if it is XML, why make it invalid for reading?
Is it? I must say the question was quite specific, he wanted to write to one file for whatever reason.

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.