views:

42

answers:

1

Hi again.

I have a list of songs which I want to output to an external XML file using a smart device (pocket pc).

String path = GetAppDir();
string filePath = path + @"\output\songs.xml";
XmlWriter xmlOut = XmlWriter.Create(filePath, settings);


xmlOut.WriteStartDocument();
xmlOut.WriteStartElement("Songs");


foreach (Song songTmp in finalbasket)
{
    xmlOut.WriteStartElement("Songs");
    xmlOut.WriteAttributeString("Name", songTmp.SongName);
    xmlOut.WriteElementString("Artist", songTmp.SongArtist);
    xmlOut.WriteElementString("Price", Convert.ToString(songTmp.SongPrice));
    xmlOut.WriteEndElement();
}


xmlOut.WriteEndElement();


xmlOut.Close();

The application seems to write the xml document but it always comes up empty. There are indeed items in the 'finalbasket' list. Any ideas what I am doing wrong?

+2  A: 

I think you need an

xmlOut.WriteEndDocument();

right before the xmlOut.Close(). Also, I'm not sure if this is part of your problem, but this line:

xmlOut.WriteStartElement("Songs");

should probably be this:

xmlOut.WriteStartElement("Song");
MusiGenesis
Still does not work. I've tried some different file names, it doesnt seem to be outputting a file at all, but there are no compilation errors. Do I have to add a file to my project I wonder?
Jon Mattias
Is this code maybe in a `try/catch` block that's silently swallowing any errors? Your `GetApplicationDirectory` method may be returning a path with a "\" at the end, which would make `filePath` invalid. I usually use `Path.Combine` in that situation, so I don't have to worry about it.
MusiGenesis
Does manually calling Flush make a difference?
ctacke
Flushing does not do anything either. The code is not in a try/catch, and the GetApplicationDirectory works just fine when I am reading in other files. I am using the exact same syntax for everything, the progam just does not output any xml.
Jon Mattias