1
新元素我有這個xml文件添加在XML
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Day id="Monday">
<subject name="Software Testing" session="2010" group="Alpha" teacher="Tasawar Khan" classroom="Class Room 1" time="8:30">
</subject>
</Day>
<Day id="Tuesday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Wednesday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Thursday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
<Day id="Friday">
<subject name="Digital Image Processing" session="2011" group="Alpha" teacher="Ali Javed" classroom="Class Room 2" time="11:30">
</subject>
</Day>
</Root>
我想在裏面元素根據ID添加主題元素。 我已經爲它編寫了這段代碼。
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();
XDocument loadedData = XDocument.Load(TeachersXMLPath);
var newElement = new XElement("subject", new XAttribute("name", subject),
new XAttribute("session", session),
new XAttribute("group", "alpha"),
new XAttribute("teacher", teacher),
new XAttribute("classroom", classroom),
new XAttribute("time", time));
isoStream.Position = 0;
loadedData.Root.Add(newElement);
loadedData.Save(isoStream);
}
現在我面臨着兩個問題。
- 此代碼沒有提供任何錯誤,但是當我打開xml文件時根本沒有添加任何數據。
- 其次這個代碼即使工作(這不是)。它會裏面的數據。我怎麼能添加它與特定的ID。
感謝您的幫助。我編輯FileMode創建。但還是一樣。在xml文件中沒有任何內容。問題是,它甚至沒有給出任何錯誤。 – user3297557