C#XML问题 [英] C# Xml question

查看:98
本文介绍了C#XML问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有照相馆闪存文件的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.

推荐答案

如果您正在寻求一种简单的方法,请参见:

http://msdn.microsoft.com/en-us/library/bb387061.aspx [ ^ ]


--------

这是对您编辑过的问题的答复.根据您发布的内容,该代码似乎还不错.保存的文件是什么样的?
If you are asking for a simple way to do this, see:

http://msdn.microsoft.com/en-us/library/bb387061.aspx[^]


--------

This is response to your edited question. Based on what you posted, the code seems alright. What does the saved file look like?


这篇关于C#XML问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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