2012-08-10 47 views
0

即時通訊嘗試待辦事項是當我把東西放在xmlTitle.Text(文本框)& xmlContent.Text(文本框)我想我的TextXML.xml將被更新,請小幫忙?可更新的新聞科與ASP.NET和XML

protected void Button1_Click(object sender, EventArgs e) 
    { 
     XmlDocument xmlfile = new XmlDocument(); 
     xmlfile.Load(Server.MapPath ("~/TestXML.xml")); 
     //create element 
     XmlElement theNewsTag = xmlfile.CreateElement("news"); 
     XmlElement theTitleTag = xmlfile.CreateElement("title"); 
     XmlElement theContentsTag = xmlfile.CreateElement("contents"); 
     //create text node 
     XmlText theTitleText = xmlfile.CreateTextNode(xmlTitle.Text); 
     XmlText theContentsText = xmlfile.CreateTextNode(xmlContent.Text); 
     //append 
     theTitleTag.AppendChild(theTitleText); 
     theContentsTag.AppendChild(theContentsText); 

     theNewsTag.AppendChild(theTitleTag); 
     theNewsTag.AppendChild(theContentsTag); 
     //save 
     xmlfile.DocumentElement.AppendChild(theNewsTag); 
     xmlfile.Save(Server.MapPath ("~/TestXML.xml")); 

    } 
+0

這就是即時通訊嘗試todo http://bit.ly/Rxfvet,但它似乎不是C#但它的asp.net :( – 2012-08-10 15:05:55

+1

它是C#。您在asp.net中使用C#或Vb。 Asp.net不是一種語言,它是一種技術。 – 2012-08-10 15:13:10

+0

像這樣使用XmlNode'XmlNode theNewsTag = xmlfile.CreateElement(「news」); XmlNode theTitleTag = xmlfile.CreateElement(「title」);' – 2012-08-10 15:18:07

回答

0

你的代碼工作,以測試它,我創建一個名字TestXml.xml

<?xml version="1.0" encoding="utf-8"?> 
<Data> 

</Data> 

和ASPX代碼

 <asp:Button ID="button" runat="server" Text="Write XML" 
    onclick="button_Click" /> 
<asp:TextBox ID="xmlContent" runat="server" /> 
<asp:TextBox ID="xmlTitle" runat="server" /> 

和按鈕的Click事件代碼

protected void button_Click(object sender, EventArgs e) 
{ 
    XmlDocument xmlfile = new XmlDocument(); 
    xmlfile.Load(Server.MapPath("~/TestXML.xml")); 
    //create element 
    XmlElement theNewsTag = xmlfile.CreateElement("news"); 
    XmlElement theTitleTag = xmlfile.CreateElement("title"); 
    XmlElement theContentsTag = xmlfile.CreateElement("contents"); 
    //create text node 
    XmlText theTitleText = xmlfile.CreateTextNode(xmlTitle.Text); 
    XmlText theContentsText = xmlfile.CreateTextNode(xmlContent.Text); 
    //append 
    theTitleTag.AppendChild(theTitleText); 
    theContentsTag.AppendChild(theContentsText); 

    theNewsTag.AppendChild(theTitleTag); 
    theNewsTag.AppendChild(theContentsTag); 
    //save 
    xmlfile.DocumentElement.AppendChild(theNewsTag); 
    xmlfile.Save(Server.MapPath("~/TestXML.xml")); 


} 
一個xml

我得到了以下輸出

<?xml version="1.0" encoding="utf-8"?> 
<Data> 
    <news> 
    <title>second1</title> 
    <contents>first1</contents> 
</news> 
</Data> 
+0

我的工作不起作用 – 2012-08-10 15:35:51

+0

它的工作現在再次感謝:)我沒有任何onclick xD抱歉讓你煩惱 – 2012-08-10 15:38:26

+0

不客氣 – 2012-08-10 15:43:58