永发信息网

C# xml覆盖写入用什么?

答案:2  悬赏:40  手机版
解决时间 2021-01-28 18:52
  • 提问者网友:嘚啵嘚啵
  • 2021-01-27 22:14
dxMXL.WriteXml(@"JUserKey.xml");
是指写入到“JUserKey.xml”中,但ian是却成了增量的写入。
我是想覆盖的写入,怎么办?
最佳答案
  • 五星知识达人网友:大漠
  • 2021-01-27 23:15
FileStream FS = new FileStream("JUserKey.xml", FileMode.Create);
System.Xml.XmlTextWriter myXml = new System.Xml.XmlTextWriter(FS, System.Text.Encoding.Default);
//在这里写你的东西
myXml.Close();
FS.Close();

看到了吗,FileMode.Create这个参数表示新建文件,如果原来有就覆盖。
全部回答
  • 1楼网友:有你哪都是故乡
  • 2021-01-28 00:39
你需要实现ixmlserializable接口来定制xml序列化。试试这样: public class gdevent : ixmlserializable{     private datetime _starttime;     private datetime _endtime;     public gdevent(datetime starttime, datetime endtime)     {         _starttime = starttime;         _endtime = endtime;     }     public gdevent()     {         _starttime = datetime.minvalue;         _endtime = datetime.minvalue;     }     public xmlschema getschema()     {         return null;     }     public void writexml(xmlwriter writer)     {         if (_starttime != datetime.minvalue)             writer.writeattributestring("starttime", _starttime.tostring("yyyy-mm-ddthh:mm:ssz"));         if (_endtime != datetime.minvalue)             writer.writeattributestring("endtime", _endtime.tostring("yyyy-mm-ddthh:mm:ssz"));     }     public void readxml(xmlreader reader)     {         string starttimestring = reader.getattribute("starttime");         if (!string.isnullorempty(starttimestring))         {             _starttime = datetime.parse(starttimestring);         }         string endtimestring = reader.getattribute("starttime");         if (!string.isnullorempty(endtimestring))         {             _endtime = datetime.parse(endtimestring);         }     } }
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯