Updating and appending XML file

S

Svein Erik

I'm creating a program which is to be used in soccer-cups. I'm going to use
a xml-file to store some values, the xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<player>
<playername></playername>
<matchesPlayed></matchesPlayed>
<matchesWon></matchesWon>
<matchesLost></matchesLost>
<goalsScored></goalsScored>
<goalsConceded></goalsConceded>
</player>
</root>

I have created an xmlhandler class, which is supposed to receive the data,
and update the xml file, but I'm stuck here..here thecode from the class:

class XMLHandler
{
private string pathTeams;
private string pathPlayers;
private XmlNodeList xmlnode;
private FileStream fs;
private XmlDocument xmldoc;

public XMLHandler()
{
pathTeams = Directory.GetCurrentDirectory() +
"\\XML\\Teams.xml";
pathPlayers = Directory.GetCurrentDirectory() +
"\\XML\\Players.xml";
}

public void WritePlayerNameToXml(ArrayList playerNames)
{
fs = new FileStream(pathPlayers, FileMode.Append,
FileAccess.Write,FileShare.Write);
xmldoc = new XmlDocument();
xmldoc.Load(fs);
xmlnode = xmldoc.SelectNodes("(//playername)");
//foreach (XmlNode node in xmlnode)
//{
// sb.AppendLine(node.InnerText);
//}


for (int i = 0; i < playerNames.Count; i++)
{
//...
}
xmldoc.Save(fs);

}
}

Can someone please help me with some code to insert the player IF he does
not exist, and how can i update the statistics on the player IF he exists?
 
J

Jay Riggs

Svein said:
I'm creating a program which is to be used in soccer-cups. I'm going to use
a xml-file to store some values, the xml file looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<root>
<player>
<playername></playername>
<matchesPlayed></matchesPlayed>
<matchesWon></matchesWon>
<matchesLost></matchesLost>
<goalsScored></goalsScored>
<goalsConceded></goalsConceded>
</player>
</root>

I have created an xmlhandler class, which is supposed to receive the data,
and update the xml file, but I'm stuck here..here thecode from the class:

class XMLHandler
{
private string pathTeams;
private string pathPlayers;
private XmlNodeList xmlnode;
private FileStream fs;
private XmlDocument xmldoc;

public XMLHandler()
{
pathTeams = Directory.GetCurrentDirectory() +
"\\XML\\Teams.xml";
pathPlayers = Directory.GetCurrentDirectory() +
"\\XML\\Players.xml";
}

public void WritePlayerNameToXml(ArrayList playerNames)
{
fs = new FileStream(pathPlayers, FileMode.Append,
FileAccess.Write,FileShare.Write);
xmldoc = new XmlDocument();
xmldoc.Load(fs);
xmlnode = xmldoc.SelectNodes("(//playername)");
//foreach (XmlNode node in xmlnode)
//{
// sb.AppendLine(node.InnerText);
//}


for (int i = 0; i < playerNames.Count; i++)
{
//...
}
xmldoc.Save(fs);

}
}

Can someone please help me with some code to insert the player IF he does
not exist, and how can i update the statistics on the player IF he exists?

Svein,

Perhaps instead to can read the XML file into a collection of player
objects, make whatever updates to whatever player object(s) need to be
done and then save the changes back to the XML file via serialization.

-Jay
 
Top