asp.net,用c#如何写入按以下格式写入xml文件 ?

来源:百度知道 编辑:UC知道 时间:2024/05/25 08:27:14
用fileupload上传的图片名称要写入xml的子节点中。。

每当上传一个图片,就创建一个名称跟上传图片名有关的子节点。
以此类推。。
就是下面这种格式!
这个用C#怎么写呢?
拜托了帮帮忙!!万谢!!!

(回答满意我追加分!)

<?xml version="1.0"?>
<gallery>
<photo name="0000" image="0000"/>
<photo name="0001" image="0001"/>
<photo name="A003" image="A003"/>

</gallery>
<photo> XXX </photo>

<photo> YYY </photo>

这种的我会写,但是节点 <> 里面的名称怎么动态改变、、

XmlDocument xd = new XmlDocument();
string xmlurl = HttpContext.Current.Server.MapPath("~/Test.xml");
xd.Load(xmlurl);
XmlElement el = xd.CreateElement("photo"); //添加person节点
el.SetAttribute("name", "0002"); //添加person节点的属性"name"
el.SetAttribute("image", "0002"); //添加person节点的属性 "image"

xd["gallery"].AppendChild(el);

xd.Save(HttpContext.Current.Server.MapPath("~/Test.xml"));

XmlElement photo= xmlDoc.CreateElement("photo"); photo.SetAttribute("name", "A003");
photo.SetAttribute("image", "A003");