问题描述
我有一个带有照相馆闪存文件的XML文件.我将在asp.net gridview中显示数据.用户可以选择添加另一张图片,并且一旦上传,代码应以编程方式更新xml文件,并在source属性中添加带有文件路径的新item标签.如何在上一个存在的"item"之后的"items"节点内添加具有属性的子元素"item".
这是我拥有的XML文件
I have a XML file that came with a photogallery flash file. I will be showing the data in asp.net gridview. The user will have an option to add another picture and once uploaded the code should programatically update the xml file and add a new item tag with the file path in the source attribute. How can I add a child element "item" with attributes within the "items" node after the last existing "item".
This is the XML file that I have
<items>
<item source="Baby 001.jpg" />
<item source="Baby 002.jpg" />
<item source="Baby 003.jpg" />
<item source="Baby 004.jpg" />
<item source="Baby 005.jpg" />
<item source="Baby 006.jpg" />
<item source="Baby 007.jpg" />
</items>
我编写的代码在最后一个项目之后的项目"外部而不是项目"内部创建项目".
The code that I have written creates "item" outside of the "items" not inside "items" after the last item.
XmlDocument xmlDoc = new XmlDocument();
string path = Server.MapPath("GallerySource/" + fileName);
xmlDoc.Load(path);
XmlNode ChildEl = xmlDoc.LastChild;
XmlNode node = xmlDoc.CreateElement("item");
XmlAttribute itemAttribute = xmlDoc.CreateAttribute("source");
node = xmlDoc.CreateElement("item");
itemAttribute = xmlDoc.CreateAttribute("source");
itemAttribute.Value = "picture.jpg";
node.Attributes.Append(itemAttribute);
xmlDoc.DocumentElement.AppendChild(node);
xmlDoc.Save(path);
谢谢.
Thanks.
推荐答案
这篇关于C#XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!