【发布时间】:2014-12-17 20:24:55
【问题描述】:
我正在尝试创建一个聊天系统,并且我希望每次用户编写按摩并按下按钮后,计算机都会向 xml 文件添加一个新元素。我已经成功地创建了一个新的按摩元素并将其添加到 xml 文件中,但问题是在我停止调试并检查 xml 文件之后,我发现它没有保存。 这是 HTML 文件
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Chats.aspx.cs" Inherits="Chats" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<script type="text/javascript">
function LoadXMLDoc(FileName) {
var xmlhttp;
xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", FileName, false);
xmlhttp.send();
return xmlhttp.responseXML;
}
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<center>
<h1> Chat </h1>
<div id="Box" style="position:relative;height:200px;width:400px;border-width:2px;border-style:solid;border-color:black">
<div id="Conversation">
<script type="text/javascript">
}
function Chat() {
xmlDoc = LoadXMLDoc("Chats.xml");
newNode = xmlDoc.createElement("massage");
xmlDoc.documentElement.appendChild(newNode);
document.getElementById("Conversation").innerHTML = xmlDoc.getElementsByTagName("massage").length;
}
</script>
</div>
<div id="Write" style="position:absolute; bottom:0;right:0;">
<input type="text" id="MassageText" />
<button id="SendMassageButton" causesvalidation="False" type="submit" onclick="Chat(); return false">Send</button>
</div>
</div>
</center>
</asp:Content>
这是 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<conversation>
<massage>
<from id="6">admin</from>
<to id="7">blabla</to>
<time>12/8/2014</time>
<text>6 to 7</text>
</massage>
<massage>
<from id="7">blabla</from>
<to id="6">admin</to>
<time>12/8/2014</time>
<text>7 to 6</text>
</massage>
<massage>
<from id="8">user1</from>
<to id="7">blabla</to>
<time>12/8/2014</time>
<text>8 to 7</text>
</massage>
</conversation>
我确定该元素已添加到 XML 文件中,因为我在添加元素后检查了存在多少按摩元素,答案是 4 而不是我在 XML 文件中实际拥有的 3。
【问题讨论】:
-
保存 XML 的代码在哪里?看起来您正在加载它并在浏览器中附加它,但没有写回“Chats.xml”的方法 - 您需要设置一种在服务器上处理新 XML 的方法。您需要向服务器写入请求以在文件系统上添加这些更改。
标签: javascript html asp.net ajax xml