Pregunta

I have an XML file like this

<?xml version="1.0" encoding="utf-8"?>
<Root>
  <Teachers>
    <Teacher>Ali Javed</Teacher>
  </Teachers>
</Root>

I am adding new element dynamically like this.

using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(TeachersXMLPath, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication()))
{
    string session = tb_session.Text.ToString();
    string subject = tb_subject.Text.ToString();
    DateTime? _datetime = val_timer.Value;
    String time = _datetime.Value.Hour + ":" + _datetime.Value.Minute;
    string crdthr = ((ListPickerItem)lst_credithr.SelectedItem).Content.ToString();
    string teacher = tb_teacher.Text.ToString();
    string classroom = tb_class.Text.ToString();
    string day_week = tb_day.Text.ToString();
    XDocument loadedData = XDocument.Load(isoStream);

    var tchElement = new XElement("Teacher");
    var tchr = loadedData.Root.Element("Teachers");
    tchr.Add(tchElement);
    tchElement.Value = teacher;
    loadedData.Save(isoStream);
    MessageBox.Show("Added");

}

But instead of adding the element it adds and also duplicates all the elements like this.

 <?xml version="1.0" encoding="utf-8"?>
        <Root>
          <Teachers>
            <Teacher>teacher 1</Teacher>
          </Teachers>
    </Root>
<?xml version="1.0" encoding="utf-8"?>
        <Root>
          <Teachers>
            <Teacher>teacher 1</Teacher>
    <Teacher>teacher 2</Teacher>
          </Teachers>
    </Root>
¿Fue útil?

Solución

It looks like what's happening here is that you are opening the stream with FileMode.Open, reading to the end of the stream to read in the XML document, and then writing to that stream starting at that point. So that's why you are encountering this duplication.

I suggest reading in the file as one access, and then reopening the file with FileMode.Create, so that it gets fully overwritten:

IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForApplication();
XDocument loadedData;
using (IsolatedStorageFileStream isoStream = 
    new IsolatedStorageFileStream(TeachersXMLPath, FileMode.Open, storageFile))
{
    loadedData = XDocument.Load(isoStream);
}
using (IsolatedStorageFileStream isoStream = 
    new IsolatedStorageFileStream(TeachersXMLPath, FileMode.Create, storageFile))
{
    string session = tb_session.Text.ToString();
    string subject = tb_subject.Text.ToString();
    DateTime? _datetime = val_timer.Value;
    String time = _datetime.Value.Hour + ":" + _datetime.Value.Minute;
    string crdthr = ((ListPickerItem)lst_credithr.SelectedItem).Content.ToString();
    string teacher = tb_teacher.Text.ToString();
    string classroom = tb_class.Text.ToString();
    string day_week = tb_day.Text.ToString();

    var tchElement = new XElement("Teacher");
    var tchr = loadedData.Root.Element("Teachers");
    tchr.Add(tchElement);
    tchElement.Value = teacher;
    loadedData.Save(isoStream);
    MessageBox.Show("Added");
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top