XML读(带注释********的是比较必要的步骤):
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类能不能实际用我也没实践过。
先到此为止