Click here to Skip to main content
15,905,232 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
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.

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



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

File Contain That Type of Data.

XML
<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>
Posted
Updated 11-Apr-11 2:52am
v3
Comments
Sandeep Mewara 11-Apr-11 9:15am    
What do you mean edit/delete?

All you need is to traverse the XML nodes and make changes. Is that you are trying to ask?

1 solution

Here is a simple code to delete a node :

C#
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");


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 :

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


And to insert a new element :

C#
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);
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900