AttributeToElement

//取出xml文件的全路径
string path = Server.MapPath("App_Data/new.xml");
//导入XML文件
XElement xe = XElement.Load(path);
//用LINQ查找要操作的元素
IEnumerable<XElement> element = from ee in xe.Elements("Person")
                                                   where ee.Attribute("IDCard").Value == "22030219771012"
                                                   && ee.Element("Name").Value == "张三"
                                                   select ee;

if (element.Count() > 0)//存在要操作的元素
{
XElement first = element.First();
//取身份证号属性
XAttribute attribute = first.Attribute("IDCard");
//添加一个名称和值都与属性一样的子元素
first.AddFirst(
new XElement(attribute.Name,attribute.Value)
);
//删除身份证号属性
first.RemoveAttributes();
}
//保存XML文件
xe.Save(path);

//在网页上显示文件内容
Response.Write(xe);
//设置网页显示的类型为XML文件
Response.ContentType = "text/xml";
Response.End();

原文地址:https://www.cnblogs.com/Yellowshorts/p/2867614.html