EDN Admin
Well-known member
hi friends. i am learning c# and was building a simple "words i like" app.
it is for stroing a word and its meaning in a xml file.
a bit of oversimplified code -
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
private void Form_Main_Load(object sender, EventArgs e)
{
FileInfo fi = new FileInfo("words.xml");
if (!fi.Exists || fi.Length == 0) initNewDataFile();
c("Successfully opened the data file.");
loadDataFile();
}
private void loadDataFile()
{
c("Loading data...");
XmlReader reader = XmlReader.Create(
File.Open("words.xml", FileMode.Open));
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "word")
{
comboBox_word.Items.Add(reader.GetAttribute("name"));
}
}
c("Readstate before close: " + reader.ReadState.ToString());
reader.Close();
c("Readstate after close: " + reader.ReadState.ToString());
c("Loading complete.");
}
private void initNewDataFile()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
//settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create("words.xml",settings);
writer.WriteStartDocument();
writer.WriteStartElement("words");
writer.WriteStartElement("word");
writer.WriteAttributeString("name", "atheist");
writer.WriteValue("A person who believes that God does not exist.");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
c("Created and initialized the data file.");
}
private void c(object o)
{ Console.WriteLine(o.ToString()); }
private void comboBox_word_SelectedIndexChanged(object sender, EventArgs e)
{
c("selected index...");
<div style="margin-left:40px //this line produces exception:IOexception :The process cannot access the file //CrojectsWords I LikebinDebugwords.xml because it is being used by //another process.
<div style="margin-left:40px <span style="color:rgb(255,0,0) XmlReader reader = XmlReader.Create(File.Open("words.xml", FileMode.Open , FileAccess.Read ));
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "word")
{
if (reader.GetAttribute("name") == (string)comboBox_word.SelectedItem)
textBox_description.Text = reader.ReadString();
}
}
reader.Close();
}
the red line above produces the IOException: "The process cannot access the file CrojectsWords I LikebinDebugwords.xml because it is being used by another process."
I cant seem to point out what is happening
btw the console output is ( by the c() )-
Successfully opened the data file.
Loading data...
Readstate before close: EndOfFile
Readstate after close: Closed
Loading complete.
selected index...
A first chance exception of type System.IO.IOException occurred in mscorlib.dll
i am no cleat about how the reader closes the file after access. any ideas?
View the full article
it is for stroing a word and its meaning in a xml file.
a bit of oversimplified code -
<div style="text-align:left
<div class=codeseg>
<div class=codecontent>
<div class=codesniptitle><span style="width:100% Code Snippet
private void Form_Main_Load(object sender, EventArgs e)
{
FileInfo fi = new FileInfo("words.xml");
if (!fi.Exists || fi.Length == 0) initNewDataFile();
c("Successfully opened the data file.");
loadDataFile();
}
private void loadDataFile()
{
c("Loading data...");
XmlReader reader = XmlReader.Create(
File.Open("words.xml", FileMode.Open));
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "word")
{
comboBox_word.Items.Add(reader.GetAttribute("name"));
}
}
c("Readstate before close: " + reader.ReadState.ToString());
reader.Close();
c("Readstate after close: " + reader.ReadState.ToString());
c("Loading complete.");
}
private void initNewDataFile()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
//settings.NewLineOnAttributes = true;
XmlWriter writer = XmlWriter.Create("words.xml",settings);
writer.WriteStartDocument();
writer.WriteStartElement("words");
writer.WriteStartElement("word");
writer.WriteAttributeString("name", "atheist");
writer.WriteValue("A person who believes that God does not exist.");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
c("Created and initialized the data file.");
}
private void c(object o)
{ Console.WriteLine(o.ToString()); }
private void comboBox_word_SelectedIndexChanged(object sender, EventArgs e)
{
c("selected index...");
<div style="margin-left:40px //this line produces exception:IOexception :The process cannot access the file //CrojectsWords I LikebinDebugwords.xml because it is being used by //another process.
<div style="margin-left:40px <span style="color:rgb(255,0,0) XmlReader reader = XmlReader.Create(File.Open("words.xml", FileMode.Open , FileAccess.Read ));
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element && reader.Name == "word")
{
if (reader.GetAttribute("name") == (string)comboBox_word.SelectedItem)
textBox_description.Text = reader.ReadString();
}
}
reader.Close();
}
the red line above produces the IOException: "The process cannot access the file CrojectsWords I LikebinDebugwords.xml because it is being used by another process."
I cant seem to point out what is happening
btw the console output is ( by the c() )-
Successfully opened the data file.
Loading data...
Readstate before close: EndOfFile
Readstate after close: Closed
Loading complete.
selected index...
A first chance exception of type System.IO.IOException occurred in mscorlib.dll
i am no cleat about how the reader closes the file after access. any ideas?
View the full article