level 1
Sxin0517
楼主
XML类库2(添加数据adddata)
public static intAddData(string path,string tableName,params XmlParamter[] param)
{
XmlDocument doc = new XmlDocument();
try
{
///导入XML文档
doc.Load(path);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
///选择根节点
XmlNode Node = doc.SelectSingleNode(“/”+tableName+”s”);
If(node == null) return -1;
///创建新纪录的ID值
Int newID = DataTypeConvertToInt(node.LastChild.Attributes[”ID”].Value)+1;
if(newID<1)return -1;
///创建一个新节点
XmlNode newNode = doc.CreateNode(XmlNodeType.Element,tableName,null);
if(newNode == null return -1);
///添加ID值
newNode.Attributes.Append(CreateNodeAttribute(doc,”ID”,newID.ToString()));
///添加新节点的属性
foreach(XmlParamter p in param)
{
newNode.Attributes.Append(CreateNodeAttribute(doc,p.Name,p.Value));
}
///将新节点追加到根节点中
node.AppendChild(newNode);
try
{ ///保存XML文档
doc.Save(path);
}
catch(Exception ex)
{
throw new Exception(ex.Message,ex);
}
return newID;
}