如何使用C#在asp.net中编辑/删除这种xml类型的记录 [英] how to edit/delete record in this type of xml in asp.net with C#

查看:65
本文介绍了如何使用C#在asp.net中编辑/删除这种xml类型的记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以告诉我如何使用c#使用asp.net编辑/删除此xml文件中的记录.我只想编辑那些行而不是整个记录.

Can any one tell me how to edit/delete record in this xml file using asp.net with c#. i just want to edit only those line not whole record.

<image imagePath="gihm/gihm1.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g1.jpg" colorTheme="0x66FF66" colorBackground="0x000000" colorDescription="0xFFFF00">
    </image>




我只是从xml文件中编辑/删除这些记录.

文件包含该类型的数据.




i am only edit/delete these record from xml file.

File Contain That Type of Data.

<slideshow>
<settings

        slideshowWidth="590"

        slideshowHeight="300"

        imageTime="5"

        transitionTime = "1"

        easeType = "easeInOutSine"

        toShuffle = "true"

        enableNavigation = "true"

        enableDescriptions = "true"

        lockDescriptionOpen = "false"

        enablePlayPauseButton = "true"

        enableImageLink = "true"

        elementsTransitionTime = ".7"

        elementsEaseType = "easeOutCirc"

    >
    </settings>
    <image imagePath="gihm/gihm1.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g1.jpg" colorTheme="0x66FF66" colorBackground="0x000000" colorDescription="0xFFFF00">
    </image>
    <image imagePath="gihm/gihm2.jpg" imageName="GIHM Student" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g2.jpg" colorTheme="0x000000" colorBackground="0xCCCCCC" colorDescription="0x333333">
    </image>
    <image imagePath="gihm/gihm3.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g3.jpg" colorTheme="0x0033CC" colorBackground="0xFFFFFF" colorDescription="0x000000">
    </image>
    <image imagePath="gihm/gihm4.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g4.jpg" colorTheme="0xFF6600" colorBackground="0x000000" colorDescription="0xFFBC2D">
    </image>
    <image imagePath="gihm/gihm5.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g5.jpg" colorTheme="0x00CC00" colorBackground="0x000000" colorDescription="0xFFFFFF">
    </image>
    <image imagePath="gihm/gihm6.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g6.jpg" colorTheme="0xFF6600" colorBackground="0x000000" colorDescription="0xFFFFFF">
    </image>
    <image imagePath="gihm/gihm7.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g7.jpg" colorTheme="0x00CC33" colorBackground="0x000000" colorDescription="0x666666">
    </image>
    <image imagePath="gihm/gihm8.jpg" imageName="Looks sweet..." imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g8.jpg" colorTheme="0x5E4675" colorBackground="0xCCCCCC" colorDescription="0x333333">
    </image>
    <image imagePath="gihm/gihm9.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g9.jpg" colorTheme="0xCCCCCC" colorBackground="0x000000" colorDescription="0x999999"></image>
    <image imagePath="gihm/gihm10.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g10.jpg" colorTheme="0xFF6600" colorBackground="0x000000" colorDescription="0xFFFFFF">
    </image>
    <image imagePath="gihm/gihm11.jpg" imageName="GIHM" imageDescription="GIHM" url="default.aspx" thumbnailPath="gihm/thumb/g11.jpg" colorTheme="0xFFFFFF" colorBackground="0x6F9BB4" colorDescription="0x000000">
    </image>
</slideshow>

推荐答案

下面是删除节点的简单代码:

Here is a simple code to delete a node :

XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"C:\Test.xml");
            XmlNode elem = xmlDoc.SelectSingleNode("/slideshow/image[position()=1]");
            XmlNode root = xmlDoc.DocumentElement;
            root.RemoveChild(elem);
            xmlDoc.Save(@"C:\Test.xml");



当您要删除第一个元素时,SelectSingleNode使用位置设置为1的Xpath表达式.



由于只有属性,因此可以按以下方式进行



The SelectSingleNode uses an Xpath expression with position set to 1 as you want to delete the first element.

To edit:

Since you only have attributes, you can edit in this way :

elem.Attributes["imagePath"].Value = "ddd";



并插入一个新元素:



And to insert a new element :

XmlElement newElement = xmlDoc.CreateElement("Image");
            XmlAttribute attr = xmlDoc.CreateAttribute("path");
            attr.Value = "Add you path here";
            newElement.Attributes.Append(attr);

            root.InsertAfter(newElement, root.LastChild);


这篇关于如何使用C#在asp.net中编辑/删除这种xml类型的记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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