Windows Phone的XML文件读写

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类能不能实际用我也没实践过。

先到此为止

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值