使用linq在xml文件中添加节点 [英] add a node in xml file using linq

查看:125
本文介绍了使用linq在xml文件中添加节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个空白的xml文件,我想在按钮单击时使用linq在其中创建一个节点.

i have a blank xml file i want to create a node in this using linq on button click

推荐答案

protected void buttonAddClick(object sender, EventArgs e)
{
    try
    {
        XDocument xmlDoc = XDocument.Load(Server.MapPath("MyDoc.xml"));
        xmlDoc.Element("Employees").Add(
        new XElement("Employee", 
        new XElement("Name", txtName.Text),
        new XElement("City", txtCity.Text), 
        new XElement("Age", txtAge.Text)));
        
        xmlDoc.Save(Server.MapPath("MyEmployees.xml"));
    }
    catch{}
}


引用此线程,可能会对您有所帮助

http://forums.asp.net/t/1470779.aspx/1 [ ^ ]
refer this thread, might help you

http://forums.asp.net/t/1470779.aspx/1[^]


GanesanSenthilvel解决方案几乎没有变化即可正常工作

GanesanSenthilvel solution works fine with little change

protected void buttonAddClick(object sender, EventArgs e)
{
    try
    {
        XDocument xmlDoc = XDocument.Load(Server.MapPath("MyDoc.xml"));
        xmlDoc.Element("Employees").Add(
        new XElement("Employee", 
        new XElement("Name", txtName.Text),
        new XElement("City", txtCity.Text), 
        new XElement("Age", txtAge.Text)));
        
        xmlDoc.Save(Server.MapPath("MyDoc.xml"));
    }
    catch{}
}


这篇关于使用linq在xml文件中添加节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆