您的位置:首页 > 编程语言 > C#

C# 添加xml的节点

2013-02-26 14:50 260 查看
首先导入

1.添加命名空间

using System.Xml;

2.声明:

private XmlDataDocument xmldoc;
3.初始化:    /// <summary>
/// 初始化xmldoc
/// </summary>
public void localxml()
{
xmldoc = new XmlDataDocument();
xmldoc.Load(GetMapPath("xmlfile/shezhi.xml"));
}


4.添加节点

/// <summary>
/// 添加xml节点。
/// </summary>
public void wirtexml(string family)
{
localxml();
string jdid = "jp" + family;
XmlNode xmlserver = xmldoc.SelectSingleNode("jiapu");
XmlElement jp_shez = xmldoc.CreateElement("jp_shezhi");
jp_shez.SetAttribute("id", jdid);
XmlElement jb_up_dw = xmldoc.CreateElement("jp_up_dw");
jb_up_dw.InnerText = "100";
jp_shez.AppendChild(jb_up_dw);
XmlElement jb_lt_rt = xmldoc.CreateElement("jp_lt_rt");
jb_lt_rt.InnerText = "80";
jp_shez.AppendChild(jb_lt_rt);
XmlElement jb_color = xmldoc.CreateElement("jp_cl");
jb_color.InnerText = "red";
jp_shez.AppendChild(jb_color);
XmlElement jb_w = xmldoc.CreateElement("jp_w");
jb_w.InnerText = "1";
jp_shez.AppendChild(jb_w);
XmlElement jb_bj = xmldoc.CreateElement("jp_bj");
jb_bj.InnerText = "1";
jp_shez.AppendChild(jb_bj);
XmlElement jb_action = xmldoc.CreateElement("jp_action");
jb_action.InnerText = "lt";
jp_shez.AppendChild(jb_action);
XmlElement jb_line = xmldoc.CreateElement("jp_line");
jb_line.InnerText = "1";
jp_shez.AppendChild(jb_line);
XmlElement po_color = xmldoc.CreateElement("po_cl");
po_color.InnerText = "red";
jp_shez.AppendChild(po_color);
XmlElement po_w = xmldoc.CreateElement("po_w");
po_w.InnerText = "1";
jp_shez.AppendChild(po_w);
XmlElement po_bj = xmldoc.CreateElement("po_bj");
po_bj.InnerText = "1";
jp_shez.AppendChild(po_bj);
XmlElement po_lt_rt = xmldoc.CreateElement("po_lt_rt");
po_lt_rt.InnerText = "10";
jp_shez.AppendChild(po_lt_rt);
XmlElement familyid = xmldoc.CreateElement("po_fid");
familyid.InnerText = family.ToString();
jp_shez.AppendChild(familyid);
xmlserver.AppendChild(jp_shez);
xmlsave();
}


5.保存

public void xmlsave()
{
xmldoc.Save(Server.MapPath(@"~/xmlfile/shezhi.xml"));
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: