xml文件类似这样<StuRecord>
<StdInfo>
<ID>234</ID>
<Name>王五</Name>
<Record>78</Record>
</StdInfo>
</StuRecord>使用的方法
public void SaveToXML(string id, string name, string record)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<StuRecord></StuRecord>");
XmlNode root = xmlDoc.SelectSingleNode("StuRecord"); //创建一个新的StdInfo节点
XmlElement stdInfo = xmlDoc.CreateElement("StdInfo");
//ID节点
XmlElement stdId = xmlDoc.CreateElement("ID");
stdId.InnerText = id;
stdInfo.AppendChild(stdId); //Name节点
XmlElement stdName = xmlDoc.CreateElement("Name");
stdName.InnerText = name;
stdInfo.AppendChild(stdName); //Record节点
XmlElement stdRecord = xmlDoc.CreateElement("Record");
stdRecord.InnerText = record;
stdInfo.AppendChild(stdRecord); //添加StdInfo节点
root.AppendChild(stdInfo); //保存xml文件
xmlDoc.Save("StuRecord.xml");
}当再次调用这个方法时,只是把上次的给<StdInfo>给修改了,而不是创建一个新的<StdInfo>节点,我如果想每次调用都创建一个新的<StdInfo>,该如何修改
<StdInfo>
<ID>234</ID>
<Name>王五</Name>
<Record>78</Record>
</StdInfo>
</StuRecord>使用的方法
public void SaveToXML(string id, string name, string record)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<StuRecord></StuRecord>");
XmlNode root = xmlDoc.SelectSingleNode("StuRecord"); //创建一个新的StdInfo节点
XmlElement stdInfo = xmlDoc.CreateElement("StdInfo");
//ID节点
XmlElement stdId = xmlDoc.CreateElement("ID");
stdId.InnerText = id;
stdInfo.AppendChild(stdId); //Name节点
XmlElement stdName = xmlDoc.CreateElement("Name");
stdName.InnerText = name;
stdInfo.AppendChild(stdName); //Record节点
XmlElement stdRecord = xmlDoc.CreateElement("Record");
stdRecord.InnerText = record;
stdInfo.AppendChild(stdRecord); //添加StdInfo节点
root.AppendChild(stdInfo); //保存xml文件
xmlDoc.Save("StuRecord.xml");
}当再次调用这个方法时,只是把上次的给<StdInfo>给修改了,而不是创建一个新的<StdInfo>节点,我如果想每次调用都创建一个新的<StdInfo>,该如何修改
try {
doc.Load(LotteryHelper.RecordFileName);
XmlNode root = doc.DocumentElement; XmlElement xe1 = doc.CreateElement("Item");
XmlAttribute node;
XmlElement element;
XmlNodeList nodeList = doc.GetElementsByTagName("Item");
if (MultiGifts <= 1) {
for (int i = 0; i < nodeList.Count; i++) {
element = (XmlElement)nodeList.Item(i);
if ((element.GetAttribute("name") == m.UserName) && (element.GetAttribute("value") == m.UserNo)) {
ret = false;
break;
}
}
}
if (ret) {
node = doc.CreateAttribute("name");
node.InnerText = m.UserName;
xe1.Attributes.Append(node); node = doc.CreateAttribute("no");
node.InnerText = m.UserNo;
xe1.Attributes.Append(node); node = doc.CreateAttribute("gift");
node.InnerText = g.GiftName;
xe1.Attributes.Append(node); node = doc.CreateAttribute("dept");
node.InnerText = m.Dept;
xe1.Attributes.Append(node); node = doc.CreateAttribute("time");
node.InnerText = System.DateTime.Now.ToString();
xe1.Attributes.Append(node);
root.AppendChild(xe1); doc.Save(LotteryHelper.RecordFileName);
}
}
catch {
ret = false;
}<?xml version="1.0" encoding="utf-8"?>
<Items>
<Item name="a" no="138882**1" gift="参与奖" dept="物流" time="2010-11-8 10:28:21" />
<Item name="何" no="138882**2" gift="参与奖" dept="发展" time="2010-11-8 10:28:23" />
<Item name="杨" no="138882**3" gift="参与奖" dept="a部" time="2010-11-8 10:31:03" />
<Item name="孔" no="138882**4" gift="参与奖" dept="b部" time="2010-11-8 10:31:05" />
</Items>
{
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
if (element.Equals(""))
{
if (!attribute.Equals(""))
{
XmlElement xe = (XmlElement)xn;
xe.SetAttribute(attribute, value);
}
}
else
{
XmlElement xe = doc.CreateElement(element);
if (attribute.Equals(""))
{
xe.InnerText = value;
}
else
{
xe.SetAttribute(attribute, value);
}
xn.AppendChild(xe);
}
doc.Save(path);
}
catch { }
}
http://topic.csdn.net/u/20090515/12/619c10d1-af12-4c90-bfcd-101da5dd8ddf.html