XML读写的问题 由于才学习XML很短的时间,现在要作一个小的销售系统,要求能够读写XML文件,并写入SQL数据库中去。请问用什么方法读写好些?请高人帮忙了,万分感谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;using System.Xml;namespace Common{ /// <summary> /// Config 的摘要说明。 /// </summary> public class Config { private String msFileName = null; public String ConfigFile { get { return this.msFileName; } set { if(System.IO.File.Exists(value.Trim())) { this.msFileName = value.Trim(); } } } public Config() { this.msFileName = String.Empty; } public Config(String ConfigFile) { this.ConfigFile = ConfigFile.Trim(); } public bool ReadConfig(String ContentName, out String ContentValue) { bool bFlag = false; ContentValue = String.Empty; if(!System.IO.File.Exists(this.msFileName)) { return bFlag; } try { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(this.msFileName); System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); ContentValue = xmlNode.InnerText; bFlag = true; } catch (XmlException xmle) { System.Console.WriteLine(xmle.Message); } return bFlag; } public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue) { bool bFlag = false; PropertyValue = String.Empty; if(!System.IO.File.Exists(this.msFileName)) { return bFlag; } try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for(int i=0; i<xmlAttr.Count; ++i) { if (xmlAttr.Item(i).Name == PropertyName) { PropertyValue = xmlAttr.Item(i).Value; bFlag = true; break; } } } catch (XmlException xmle) { System.Console.WriteLine(xmle.Message); } return bFlag; } public bool WriteConfig(String ContentName, String ContentValue) { bool bFlag = false; if (!System.IO.File.Exists(this.msFileName)) { return bFlag; } try { System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument(); xmlDoc.Load(this.msFileName); System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); xmlNode.InnerText = ContentValue; xmlDoc.Save(this.msFileName); bFlag = true; } catch (XmlException xmle) { System.Console.WriteLine(xmle.Message); } return bFlag; } public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue) { bool bFlag = false; if (!System.IO.File.Exists(this.msFileName)) { return bFlag; } try { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for (int i = 0; i < xmlAttr.Count; ++i) { if (xmlAttr.Item(i).Name == PropertyName) { xmlAttr.Item(i).Value = PropertyValue; bFlag = true; break; } } xmlDoc.Save(this.msFileName); bFlag = true; } catch (XmlException xmle) { System.Console.WriteLine(xmle.Message); } return bFlag; } }} cscer(时光之石头),你好,能不能解释一下这段代码啊?谢谢了。 楼上说得对用DATASET读写很方便,如果要求不高的直接那样读写XML就可以了 通过DataSet,//写xmlthisDataSet.WriteXml(filename,XmlWriteMode.DiffGram );//读xmlthisDataSet.ReadXml(filename,XmlReadMode.DiffGram ); 更新数据库使用DataAdapter。 怎么使用tcelee() ( )的XML读取,与写入的这个类啊. 请问如何获得页面的浏览次数 如何被动检测程序的运行? DataView问题,急急急!!!!!!!!! 关于dataset插入提交到数据库的问题 请问在PDA上如何用程序打开蓝牙端口 C# 访问framem内容,帮忙看下 我是一个菜鸟!大家帮我看一下!这存储过程哪里错了 如何将页面中的内容按照指定的样式导出为PPT格式 问:怎样使用Singleton模式使得整个应用程序中只有一个连接数据库的Connection实例? win2000下怎么将FAT32转换成NTFS? 为什么不能建立web项目 超级郁闷问题,解决马上给分!!!
using System.Xml;namespace Common
{
/// <summary>
/// Config 的摘要说明。
/// </summary>
public class Config
{
private String msFileName = null; public String ConfigFile
{
get
{
return this.msFileName;
}
set
{
if(System.IO.File.Exists(value.Trim()))
{
this.msFileName = value.Trim();
}
}
} public Config()
{
this.msFileName = String.Empty;
} public Config(String ConfigFile)
{
this.ConfigFile = ConfigFile.Trim();
} public bool ReadConfig(String ContentName, out String ContentValue)
{
bool bFlag = false; ContentValue = String.Empty; if(!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
ContentValue = xmlNode.InnerText; bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
} public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
{
bool bFlag = false; PropertyValue = String.Empty; if(!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for(int i=0; i<xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
PropertyValue = xmlAttr.Item(i).Value;
bFlag = true;
break;
}
}
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
} public bool WriteConfig(String ContentName, String ContentValue)
{
bool bFlag = false; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
xmlNode.InnerText = ContentValue; xmlDoc.Save(this.msFileName); bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
} public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
{
bool bFlag = false; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
xmlAttr.Item(i).Value = PropertyValue;
bFlag = true;
break;
}
} xmlDoc.Save(this.msFileName); bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
}
}
}
//写xml
thisDataSet.WriteXml(filename,XmlWriteMode.DiffGram );
//读xml
thisDataSet.ReadXml(filename,XmlReadMode.DiffGram );
更新数据库使用DataAdapter。