string xmlpath = Application.StartupPath; try { if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")) {string xmlpath = Application.StartupPath;try{if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")){XmlDocument doc = new XmlDocument();doc.Load(xmlpath + @"\
来源:学生作业帮助网 编辑:六六作业网 时间:2024/11/22 10:03:33
string xmlpath = Application.StartupPath; try { if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")) {string xmlpath = Application.StartupPath;try{if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")){XmlDocument doc = new XmlDocument();doc.Load(xmlpath + @"\
string xmlpath = Application.StartupPath; try { if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")) {
string xmlpath = Application.StartupPath;
try
{
if(File.Exists(xmlpath+@"\"+"StoreHistory.xml"))
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlpath + @"\" + "StoreHistory.xml");
XmlElement xe = doc.DocumentElement;
XmlElement newhistory=doc.CreateElement("history");
XmlElement newtime=doc.CreateElement("time");
XmlElement newtitle=doc.CreateElement("title");
XmlElement newweb = doc.CreateElement("web");
XmlText time = doc.CreateTextNode(Time);
XmlText title = doc.CreateTextNode(Title);
XmlText web = doc.CreateTextNode(Website);
newhistory.AppendChild(newtime);
newhistory.AppendChild(newtitle);
newhistory.AppendChild(newweb);
newtime.AppendChild(time);
newtitle.AppendChild(title);
newweb.AppendChild(web); xe.InsertAfter(newhistory,xe.LastChild);
doc.Save(xmlpath + @"\" + "StoreHistory.xml");
}
保存网页的历史记录代码 3Q
{
XmlWriter xwr = null;
try
{
XmlWriterSettings
settings = new XmlWriterSettings();
settings.Indent =
true;
settings.IndentChars
= (" ");
xwr =
XmlWriter.Create("StoreHistory.xml",settings);
xwr.WriteStartElement("Root");
xwr.WriteStartElement("history");
xwr.WriteStartElement("time");
xwr.WriteValue(Time);
xwr.WriteEndElement(); xwr.WriteStartElement("title");
xwr.WriteValue(Title);
xwr.WriteEndElement();
xwr.WriteStartElement("web"); xwr.WriteValue(Website);
xwr.WriteEndElement();
xwr.WriteEndElement();
xwr.WriteEndElement();
}解释得很清楚 这段是接着上一段的假如不存在那个xml文件
string xmlpath = Application.StartupPath; try { if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")) {string xmlpath = Application.StartupPath;try{if(File.Exists(xmlpath+@"\"+"StoreHistory.xml")){XmlDocument doc = new XmlDocument();doc.Load(xmlpath + @"\
功能:读取xml文件并修改(加入一些节点).
Application.StartupPath --》bin文件夹的路径
file.exist(文件路径)--》判断文件是否存在.
XmlDocument.CreateElement 创建元素
XmlDocument doc = new XmlDocument();//新建一空xml对象,
XmlElement xe = doc.DocumentElement;//赋值(从源文件),
XmlElement newhistory=doc.CreateElement("history"); //新建元素(节点)
newhistory.AppendChild(newtime); //添加子元素(节点)
newhistory.AppendChild(newweb); //添加子元素(节点)
xe.InsertAfter(newhistory,xe.LastChild); //在末尾插入此节点
————————————————————
以上代码在xml中添加一个history元素,结果如下:
----》XmlElement newhistory=doc.CreateElement("history");
--》newhistory.AppendChild(newtime);
————————————
补充问题:
------>>xwr.WriteStartElement("Root");
----->>xwr.WriteStartElement("history");
--->>xwr.WriteStartElement("time");
Time 的值 --->>xwr.WriteStartElement("time");
--->>xwr.WriteEndElement();
Title的值 ...
Web的值 ...
-->>xwr.WriteEndElement();
-->>xwr.WriteEndElement();
-------
XmlWriterSettings
settings = new XmlWriterSettings();
settings.Indent =true; ----->>允许缩进
settings.IndentChars = (" "); ---》缩进字符为空格
天.你后面不会还有吧?