![](/img/trans.png)
[英]Is a DOM element available immediately after it has been added to the DOM tree?
[英]xml file does not save after element has been added
我正在嘗試創建一個聊天系統,我希望用戶每次寫完按摩並按下按鈕后,計算機都會向xml文件添加一個新元素。 我已經成功創建了一個新的Massage元素並將其添加到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文件中,因為我在添加元素后檢查了多少個Massages元素,並且答案是4,而不是XML文件中實際包含的3。
請參閱此處以保存文檔:
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.CreateElement("item","urn:1"));
doc.Save(Console.Out);
http://msdn.microsoft.com/zh-cn/library/system.xml.xmldocument.save(v=vs.110).aspx
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.