0

我只想知道如何在我的 XML 上添加一个具有 removechild() 或 replacechild() 函数的按钮。一个星期以来一直在寻找这个。

任何建议将不胜感激。谢谢 :)

这是我的 xml 文件内容

 <?xml version="1.0" encoding="utf-8"?>
 <theNews>
 <news>
 <contents>News3 Contents</contents>
 <title>News3 Title</title>
 <pubDate>February 13, 2012</pubDate>
 </news>
 <news>
 <contents>News2 Contents</contents>
 <title>News2 Title</title>
 <pubDate>February 1, 2012</pubDate>
 </news>
 <news>
 <contents>News1 Contents</contents>
 <title>News1 Title</title>
 <pubDate>January 22, 2012</pubDate>
 </news>
 </theNews>

这是我的新闻更新程序的代码

<%@ Page Title="" Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="NewsUpdater.aspx.vb" Inherits="NewsUpdater" %>

 <%-- Add content controls here --%><asp:Content ID="Content1" runat="server" 
contentplaceholderid="ContentPlaceHolder1">

        <p>
            &nbsp;</p>
        <table align="center" style="width: 63%">
            <tr>
                <td align=center>
                    <div>
    <h3>News Title</h3>
    <asp:TextBox runat="server" ID="txtTitle" Width="308px"></asp:TextBox>
                        <br />
    <h3>News Content</h3>
    <asp:TextBox runat="server" ID="txtContents" TextMode="MultiLine" Height="119px" Width="513px"></asp:TextBox>


                        <br />

                        <br />
                        <asp:Button ID="Button1" runat="server" Text="Update News" />
</div></td>
            </tr>
        </table>
        <p>
            <br />
        </p>

这是 NewsUpdater 按钮背后的代码

 Partial Class NewsUpdater
 Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim xmlFile As New System.Xml.XmlDocument()
    xmlFile.Load("D:\My Documents\PresentableII\NewsContent.xml")

    Dim theNewsTag As System.Xml.XmlElement = xmlFile.CreateElement("news")
    Dim theTitleTag As System.Xml.XmlElement = xmlFile.CreateElement("title")
    Dim theContentsTag As System.Xml.XmlElement = xmlFile.CreateElement("contents")

    Dim theTitleText As System.Xml.XmlText = xmlFile.CreateTextNode(txtTitle.Text)
    Dim theContentsText As System.Xml.XmlText = xmlFile.CreateTextNode(txtContents.Text)

    theTitleTag.PrependChild(theTitleText)
    theContentsTag.PrependChild(theContentsText)


    theNewsTag.PrependChild(theTitleTag)
    theNewsTag.PrependChild(theContentsTag)

    xmlFile.DocumentElement.PrependChild(theNewsTag)
    xmlFile.Save("D:\My Documents\PresentableII\NewsContent.xml")

    Response.Redirect("NewsFrame.aspx")

End Sub

我想添加另一个按钮,该按钮具有删除或替换 xml 的最后/最新条目的功能。

4

1 回答 1

0

那么这条线会做你想要的吗?

xmlFile.DocumentElement.RemoveChild(xmlFile.DocumentElement.LastChild)
xmlFile.DocumentElement.AppendChild(your_replacement_element)
于 2012-02-15T13:38:56.210 回答