static public house getitems(string mac)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();//**************
if (!isf.FileExists(path + mac + "_WP.xml"))
return null;
house h = new house();
//mac & item build and add inside
h.mac = mac;
FileStream xrs = isf.OpenFile(path + h.mac + "_WP.xml", FileMode.Open, FileAccess.Read);<span style="font-family: Arial, Helvetica, sans-serif;">//**************</span>
XmlReader xr = XmlReader.Create(xrs);<span style="font-family: Arial, Helvetica, sans-serif;">//**************必須用Stream參數纔行,我用文件名string做參數會出現exception,不清楚原因。</span>
xr.Read();//skip the header<span style="font-family: Arial, Helvetica, sans-serif;">//*************</span>
while (xr.Read())
{
item temp_item = new item();
temp_item.name = xr.LocalName;
for (int i = 0; i < xr.AttributeCount; i++)
{
xr.MoveToNextAttribute();
temp_item.attributes.Add(xr.LocalName, xr.GetAttribute(i));
}
h.items.Add(temp_item);
}
xr.Close();<span style="font-family: Arial, Helvetica, sans-serif;">//*************</span>
xrs.Close();<span style="font-family: Arial, Helvetica, sans-serif;">//*************</span>
return h;
}
XML寫:(同上*爲必要步驟)
static public house saveitems(house h)
{
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();//*******************
isf.CreateFile(path + h.mac + "_WP_temp.xml").Close();
Stream xws = isf.OpenFile(path + h.mac + "_WP_temp.xml", FileMode.Open, FileAccess.Write);//*******************
XmlWriter xw = XmlWriter.Create(xws);//*******************
xw.WriteStartDocument();//*******************
foreach (item temp_item in h.items)
{
xw.WriteStartElement(temp_item.name);
foreach (var attr_value in temp_item.attributes)
{
xw.WriteAttributeString(attr_value.Key, attr_value.Value);
}
xw.WriteEndElement();
}
xw.WriteEndDocument();//*******************
xw.Close();//*******************
xws.Close();//*******************
if (isf.FileExists(path + h.mac + "_WP.xml"))
isf.DeleteFile(path + h.mac + "_WP.xml");
isf.CopyFile(path + h.mac + "_WP_temp.xml", path + h.mac + "_WP.xml");
isf.DeleteFile(path + h.mac + "_WP_temp.xml");
return h;
}
因爲現在並不是閒情逸致細談之時,所以我就只貼了代碼,沒有細講,而其中的XMLReader , XMLWriter等類的使用方法就先去別處找吧。還有在WINDOWSPHONE裏面這裏用的文件是IsolatedStorageFile類而不是File類,File類能不能實際用我也沒實踐過。
先到此爲止