0

XML に removechild() または replacechild() 関数を持つボタンを追加する方法を知りたいだけです。これを1週間探しています。

どんな提案でも大歓迎です。ありがとう :)

これは私の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>

これは私の News Updater のコードです

<%@ 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 に答える