locked
Search XML file *based* on search criteria and update node RRS feed

  • Question

  • User944339287 posted

    hi guys..  how can i update xml file by specific ID (1111, 1112)?

    update

            Dim xmldoc As New XmlDocument()
            xmldoc.Load(Server.MapPath("~/pages/News.xml"))
            Dim xmlnode As XmlNodeList = xmldoc.SelectNodes("/Setting/Details[contains(ID, '" + Request.QueryString("ID") + "')]")
    
            xmlnode("id").InnerText = Me.txt_ID.Text
            xmlnode("seo").InnerText = Me.txt_seo.Text
            xmlnode("type").InnerText = Me.ddl_type.SelectedValue
            xmlnode("posted-date").InnerText = Me.txt_posted_date.Text
            xmlnode("priority").InnerText = Me.ddl_priority.SelectedValue
    
            xmlnode("name").InnerText = Me.txt_name.Text
            xmlnode("title").InnerText = Me.txt_title.Text
            xmlnode("summary").InnerText = Me.txt_summary.Text
            xmlnode("content").InnerText = Me.CKEditor.Text
            xmlnode("image").InnerText = ""
    
            xmldoc.Save(Server.MapPath("~/pages/News.xml"))

    my xml file..

    <?xml version="1.0" standalone="yes"?>
    <Setting>
      <Details>
        <id>1111</id>
        <seo>lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit</seo>
        <type>text-and-images</type>
        <posted-date>2015-03-01</posted-date>
        <priority>1</priority>
        <name>admin</name>
        <title>Test 1</title>
        <summary>Nullam vel dui quis ex fermentum feugiat lobortis at velit.</summary>
        <image>/lib/dummy/01.jpg</image>
        <content>You can add some content here...</content>
      </Details>
      <Details>
        <id>1112</id>
        <seo>lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit</seo>
        <type>text-and-images</type>
        <posted-date>2015-03-02</posted-date>
        <priority>2</priority>
        <name>admin</name>
        <title>Test 2</title>
        <summary>Nullam vel dui quis ex fermentum feugiat lobortis at velit. </summary>
        <image>/lib/dummy/02.jpg</image>
        <content>You can add some content here...</content>
      </Details>
    
    </Setting>

    Tuesday, March 24, 2015 1:58 AM

Answers

  • User61956409 posted

    Hi kengkit,

    Thanks for your post.

    I could refer to the following sample to update XML data.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table>
                    <tr>
                        <td>id:</td>
                        <td>
                            <asp:TextBox ID="txtid" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>newid:</td>
                        <td>
                            <asp:TextBox ID="txtnewid" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>seo:</td>
                        <td>
                            <asp:TextBox ID="txtseo" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>type:</td>
                        <td>
                            <asp:TextBox ID="txttype" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>posted-date:</td>
                        <td>
                            <asp:TextBox ID="txtposteddate" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>priority:</td>
                        <td>
                            <asp:TextBox ID="txtpriority" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>name:</td>
                        <td>
                            <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>title:</td>
                        <td>
                            <asp:TextBox ID="txttitle" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>summary:</td>
                        <td>
                            <asp:TextBox ID="txtsummary" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>image:</td>
                        <td>
                            <asp:TextBox ID="txtimage" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>content:</td>
                        <td>
                            <asp:TextBox ID="txtcontent" runat="server"></asp:TextBox></td>
                    </tr>
                </table>
                <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" />
            </div>
        </form>
    </body>
    </html> 
    
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        XDocument xdoc = XDocument.Load(Server.MapPath("Details.xml"));
    
        foreach (var element in xdoc.Descendants("Details"))
        {
            string id=element.Element("id").Value.ToString();
            string upid = txtid.Text.ToString();
    
            if (id == upid)
            {
                element.Element("id").Value = txtnewid.Text.ToString();
                element.Element("seo").Value = txtseo.Text.ToString();
                element.Element("type").Value = txttype.Text.ToString();
                element.Element("posted-date").Value = txtposteddate.Text.ToString();
                element.Element("priority").Value = txtpriority.Text.ToString();
    
                element.Element("name").Value = txtname.Text.ToString();
                element.Element("title").Value = txttitle.Text.ToString();
                element.Element("summary").Value = txtsummary.Text.ToString();
                element.Element("image").Value = txtimage.Text.ToString();
                element.Element("content").Value = txtcontent.Text.ToString();
            }  
        }
    
        xdoc.Save(Server.MapPath("Details.xml"));
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 2:18 AM

All replies

  • User-1923420989 posted

    use linq to xml to update the xml efficiently.

    sample code ( not tested )
    
    var myDet = from det in xmlDoc.Descendants("Details")
        where det.Element("id").Value == 1111 select det;
    
    foreach (XElement itemElement in myDet)
    {
        itemElement.SetElementValue("seo", "Akbar");
    }
    
    Tuesday, March 24, 2015 2:22 AM
  • User61956409 posted

    Hi kengkit,

    Thanks for your post.

    I could refer to the following sample to update XML data.

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
            <div>
                <table>
                    <tr>
                        <td>id:</td>
                        <td>
                            <asp:TextBox ID="txtid" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>newid:</td>
                        <td>
                            <asp:TextBox ID="txtnewid" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>seo:</td>
                        <td>
                            <asp:TextBox ID="txtseo" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>type:</td>
                        <td>
                            <asp:TextBox ID="txttype" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>posted-date:</td>
                        <td>
                            <asp:TextBox ID="txtposteddate" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>priority:</td>
                        <td>
                            <asp:TextBox ID="txtpriority" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>name:</td>
                        <td>
                            <asp:TextBox ID="txtname" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>title:</td>
                        <td>
                            <asp:TextBox ID="txttitle" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>summary:</td>
                        <td>
                            <asp:TextBox ID="txtsummary" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>image:</td>
                        <td>
                            <asp:TextBox ID="txtimage" runat="server"></asp:TextBox></td>
                    </tr>
                    <tr>
                        <td>content:</td>
                        <td>
                            <asp:TextBox ID="txtcontent" runat="server"></asp:TextBox></td>
                    </tr>
                </table>
                <asp:Button ID="btnupdate" runat="server" Text="Update" OnClick="btnupdate_Click" />
            </div>
        </form>
    </body>
    </html> 
    
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        XDocument xdoc = XDocument.Load(Server.MapPath("Details.xml"));
    
        foreach (var element in xdoc.Descendants("Details"))
        {
            string id=element.Element("id").Value.ToString();
            string upid = txtid.Text.ToString();
    
            if (id == upid)
            {
                element.Element("id").Value = txtnewid.Text.ToString();
                element.Element("seo").Value = txtseo.Text.ToString();
                element.Element("type").Value = txttype.Text.ToString();
                element.Element("posted-date").Value = txtposteddate.Text.ToString();
                element.Element("priority").Value = txtpriority.Text.ToString();
    
                element.Element("name").Value = txtname.Text.ToString();
                element.Element("title").Value = txttitle.Text.ToString();
                element.Element("summary").Value = txtsummary.Text.ToString();
                element.Element("image").Value = txtimage.Text.ToString();
                element.Element("content").Value = txtcontent.Text.ToString();
            }  
        }
    
        xdoc.Save(Server.MapPath("Details.xml"));
    } 
    

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 25, 2015 2:18 AM