What's The Most Efficient Way To Create XML Files?

Hi Guys,
I’ve had a crack at creating a windows desktop application to convert a csv into an xml file, and for a beginner I think I did a pretty good job.

I tried to use LINQ which building the structure, but noticed that it only worked on complete tags, it didn’t seem to let me specify part of a document.

So instead, I used LINQ on complete tags, but where I had to leave it open to perform some loops and if statements, I had to create a string that looked like xml, not really ideal as I had to manually do all of the indentations and open and close speech marks all over the place.

I should point out that I’m not trying to create a static xml file.

If the structure is something like the following:

<root>
<firstTag></firstTag>
<secondTag></secondTag>
<thirdTag>
<forthTag></forthTag>
<forthTag></forthTag>
<forthTag></forthTag>
</thirdTag>
</root>

I don’t know in advance how many "forthTag"s will appear between the "thirdTag"s, so I know that from “root” to “thirdTag” is static, but beyond that, I need to run a lot of code to see how many times I should repeat the forthTag.

I’m not sure if I’ve put forward my question very well, but in summary, I want to create xml using LINQ but without being forced to close my tags.

I’ve got the program working perfectly well, but I’d like to learn to do it the most efficient way possible.

Any help would be appreciated.

I would use an XmlWriter. You can use this link: http://dotnetperls.com/xmlwriter or just google it. There should be tons of examples on the net.

That appears to be a c# xmlwriter, I’m using vb.net

I have a quick look on google and came across a few msdn articles that show that I might be able to make use of the xmlWriter as you said:

writer.WriteStartElement(“Stock”)
writer.WriteAttributeString(“Symbol”, symbol)
writer.WriteElementString(“Price”, XmlConvert.ToString(price))
writer.WriteElementString(“Change”, XmlConvert.ToString(change))
writer.WriteElementString(“Volume”, XmlConvert.ToString(volume))
writer.WriteEndElement()

Could I put loops (Or any other code I want) between the writeStartElement and writeEndElement lines of code?

I imagine I could as it seems to just be outputting an xml-like string.

So if I wanted to create a complete tag I would use WriteElementString, but if I want to create an open ended tag I would use the writestartelement and writestopelement

You are correct.

Cheers,
D.