blah blah blah is here! blah blah » Close

up0down
link

Please help me to solve this problem.

I've a method to write (Thank's to Mr Vulpes) and read Xml document but i put it in a single process, here is the code :

void createXMLConnection(){
int valfc=0;
int countfc=0;
if(!File.Exists("connection.xml"))
{
XmlWriterSettings setting = new XmlWriterSettings();
setting.Indent = true;
Stream xmlfile = new FileStream("connection.xml",FileMode.Create);
XmlWriter writer = XmlWriter.Create(xmlfile,setting);
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
xmlfile.Close();
}

XmlDocument doc = new XmlDocument();
doc.Load("connection.xml");
XmlElement koneksi = doc.CreateElement("Koneksi");
XmlElement nama = doc.CreateElement("Nama");

if(fm2.txtNamaKoneksi.Text==""){
XmlReader reader = XmlReader.Create("connection.xml");
reader.MoveToContent();
while (reader.Read()) {
countfc++;
if (reader.NodeType== XmlNodeType.Text) {
if (reader.Value=="Fast Connection"+countfc) {
valfc++;
}
}
}
nama.InnerText = "Fast Connection"+valfc;

}else{
nama.InnerText = fm2.txtNamaKoneksi.Text;
}
XmlElement host = doc.CreateElement("Host");
host.InnerText = txtFTPAddress.Text;
XmlElement username = doc.CreateElement("Username");
username.InnerText = txtUsername.Text;
XmlElement password = doc.CreateElement("Password");
password.InnerText = txtPassword.Text;
koneksi.AppendChild(nama);
koneksi.AppendChild(host);
koneksi.AppendChild(username);
koneksi.AppendChild(password);
doc.DocumentElement.AppendChild(koneksi);
doc.Save("connection.xml");
}


After executing the code, it shows an error something like this

The process cannot access the file because it is being used by another process

last answered one year ago

1 answers

up0down
link

The problem here is that you're trying to open the XML file with the XMLReader when it's already been opened by the XMlDocument object.

To fix it you'll need to move the code around a bit like this:

void createXMLConnection()
{
int valfc = 0;
int countfc = 0;
if(!File.Exists("connection.xml"))
{
XmlWriterSettings setting = new XmlWriterSettings();
setting.Indent = true;
Stream xmlfile = new FileStream("connection.xml",FileMode.Create);
XmlWriter writer = XmlWriter.Create(xmlfile,setting);
writer.WriteStartDocument();
writer.WriteStartElement("Root");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
xmlfile.Close();
}

if(fm2.txtNamaKoneksi.Text=="")
{
XmlReader reader = XmlReader.Create("connection.xml");
reader.MoveToContent();
while (reader.Read())
{
countfc++;
if (reader.NodeType == XmlNodeType.Text)
{
if (reader.Value == "Fast Connection" + countfc)
{
valfc++;
}
}
}
reader.Close();
}

XmlDocument doc = new XmlDocument();
doc.Load("connection.xml");
XmlElement koneksi = doc.CreateElement("Koneksi");
XmlElement nama = doc.CreateElement("Nama");

if(fm2.txtNamaKoneksi.Text=="")
{
nama.InnerText = "Fast Connection" + valfc;
}
else
{
nama.InnerText = fm2.txtNamaKoneksi.Text;
}
XmlElement host = doc.CreateElement("Host");
host.InnerText = txtFTPAddress.Text;
XmlElement username = doc.CreateElement("Username");
username.InnerText = txtUsername.Text;
XmlElement password = doc.CreateElement("Password");
password.InnerText = txtPassword.Text;
koneksi.AppendChild(nama);
koneksi.AppendChild(host);
koneksi.AppendChild(username);
koneksi.AppendChild(password);
doc.DocumentElement.AppendChild(koneksi);
doc.Save("connection.xml");
}

:-) very helpful thank you very much Mr. Vulpes Hope that you'll never get bored to help us :-)

Feedback