????? 這個教程是用Visual Studio.net 2008建立,也可以使用VS2005,但你需要從這里下載安裝Microsoft's ASP.NET AJAX Extensions,AJAX和LINQ是微軟目前主要焦點,兩個看上去不足為奇,但背后都隱藏著巨大的潛力和力量。在這個示例中,我們將看到如何用AJAX同LINQ,XML創建一個Web應用程序,我們能用看到瞬間存儲數據,也能瞬間增加數據。AJAX提供簡單易用的方式平穩高效地創建應用程序。如果是一個桌面應用程序,而LINQ提供了一種方式與XML文件和數據存儲進行交流和互動。
??? 首先,我們需要做的是創建我們的XML文件。對于這個例子中,我們將使用像這樣:
<Persons>?
<Person>?
<Name>Paxton</Name>
<City>Munich</City>
<Age>29</Age>
</Person>
<Person>?
<Name>Mike</Name>
<City>Orlando</City>
<Age>33</Age>
</Person>
</Persons>
??? 我們將創建一種既讀取和寫入XML文件:我們將需要三個Textbox和一個增加的Button,然后是另一個button和一個用于讀去的textbox。我們將構建一個這樣類似的Form:
<strong>Add?to?XML</strong><br?/>
Name:<br?/>
<asp:TextBox?ID="txtName"?runat="server"?/><br?/>
City:<br?/>
<asp:TextBox?ID="txtCity"?runat="server"?/><br?/>
Age:<br?/>
<asp:TextBox?ID="txtAge"?runat="server"?/><br?/>
<asp:Button?ID="butAdd"?runat="server"?Text="Add"?onclick="butAdd_Click"?/><br?/>
<asp:Label?ID="lblStatus"?runat="server"?/>
<br?/><br?/>
<strong>Read?XML:</strong><br?/>
<asp:Button?ID="butRead"?runat="server"?Text="Read"?onclick="butRead_Click"?/><br?/>
<asp:TextBox?ID="txtResults"?runat="server"?Columns="25"?Rows="10"?
TextMode="MultiLine"?/>
</form>?
??? 注意那是我們的buttons有一個OnClick 句柄指向一個方法。我們將這幾分鐘完成,但首先,讓我們完成我們的ASPX頁面。剩下的事兒是使頁面的AJAX生效。我們增加一個ScriptManager和一個UpdatePanel,像這樣:
<asp:ScriptManager?id="ScriptManager1"?runat="server"?/>
<asp:UpdatePanel?ID="updAdd"?runat="server">
<Triggers>?
<asp:AsyncPostBackTrigger?ControlID="butAdd"?EventName="Click"?/>
</Triggers>
<ContentTemplate>?
<strong>Add?to?XML</strong><br?/>
Name:<br?/>
<asp:TextBox?ID="txtName"?runat="server"?/><br?/>
City:<br?/>
<asp:TextBox?ID="txtCity"?runat="server"?/><br?/>
Age:<br?/>
<asp:TextBox?ID="txtAge"?runat="server"?/><br?/>
<asp:Button?ID="butAdd"?runat="server"?Text="Add"?onclick="butAdd_Click"?/><br?/>
<asp:Label?ID="lblStatus"?runat="server"?/>
<br?/><br?/>
<strong>Read?XML:</strong><br?/>
<asp:Button?ID="butRead"?runat="server"?Text="Read"?onclick="butRead_Click"?/><br?/>
<asp:TextBox?ID="txtResults"?runat="server"?Columns="25"?Rows="10"?
TextMode="MultiLine"?/>
</ContentTemplate>
</asp:UpdatePanel>
</form>
?? 在我們開始編碼之前,我們應該務必使用正確的名稱空間。我們將使用LINQ TO XML,所以我們需要 System.Xml.Linq。代碼看上去像這樣:
using?System.Configuration;
using?System.Data;
using?System.Linq;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Xml.Linq;
??? 現在回到Buttons,我們已經增加了一個Button的句柄,所以我們現在完成了ASPX頁面,現在可以編寫我們方法了,我們將創建一個讀去XML文件的方法,這個方法使一次或更多(你過會兒將看其它button的Click就知道為什么了)。我們的方法寫成像這樣:
????????{
????????????XDocument?xmlDoc?=?XDocument.Load(Server.MapPath("People.xml"));
????????????var?persons?=?from?person?in?xmlDoc.Descendants("Person")
??????????????????????????select?new
??????????????????????????{
??????????????????????????????Name?=?person.Element("Name").Value,
??????????????????????????????City?=?person.Element("City").Value,
??????????????????????????????Age?=?person.Element("Age").Value,
??????????????????????????};
????????????txtResults.Text?=?"";
????????????foreach?(var?person?in?persons)
????????????{
????????????????txtResults.Text?=?txtResults.Text?+?"Name:?"?+?person.Name?+?"\n";
????????????????txtResults.Text?=?txtResults.Text?+?"City:?"?+?person.City?+?"\n";
????????????????txtResults.Text?=?txtResults.Text?+?"Age:?"?+?person.Age?+?"\n\n";
????????????}
????????????if?(txtResults.Text?==?"")
????????????????txtResults.Text?=?"No?Results.";
????????}
這方法使用了LINQ連接XML文件,然后選擇文件里全部數據。一個選取,我們循環每條“record”輸出到textbox控件。我們這種方法為button的Click的事件:
????????{
????????????readXML();
????????????lblStatus.Text?=?"";
????????}
接下來是增加數據到XML文件。這實際上需要比讀去更少的代碼量,我們將在button的click事件下直接編碼。就這兒一樣:
????????{
????????????try
????????????{
????????????????XDocument?xmlDoc?=?XDocument.Load(Server.MapPath("People.xml"));
??????????????
????????????????xmlDoc.Element("Persons").Add(new?XElement("Person",?new?XElement("Name",?txtName.Text),
????????????????new?XElement("City",?txtCity.Text),?new?XElement("Age",?txtAge.Text)));
????????????????xmlDoc.Save(Server.MapPath("People.xml"));
????????????????lblStatus.Text?=?"Data?successfully?added?to?XML?file.";
????????????????readXML();
????????????}
????????????catch
????????????{
????????????????lblStatus.Text?=?"Sorry,?unable?to?process?request.?Please?try?again.";
????????????}
????????}
我們使用try,catch語句減少錯誤處理。接著,我們使用LINQ增加數據到XML文件。當我們第一次加載文件時,然后我們簡單增加了一個新的element到父element,請看到前面提及XML結構有助于理解。
完整的code-behind是這樣的:


using?System;
using?System.Collections.Generic;
using?System.Linq;
using?System.Web;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Xml.Linq;
namespace?THttpModule.LinqtoXml
{
????public?partial?class?Add?:?System.Web.UI.Page
????{
????????protected?void?Page_Load(object?sender,?EventArgs?e)
????????{
????????}
????????protected?void?butRead_Click(object?sender,?EventArgs?e)
????????{
????????????readXML();
????????????lblStatus.Text?=?"";
????????}
????????protected?void?butAdd_Click(object?sender,?EventArgs?e)
????????{
????????????try
????????????{
????????????????XDocument?xmlDoc?=?XDocument.Load(Server.MapPath("People.xml"));
??????????????
????????????????xmlDoc.Element("Persons").Add(new?XElement("Person",?new?XElement("Name",?txtName.Text),
????????????????new?XElement("City",?txtCity.Text),?new?XElement("Age",?txtAge.Text)));
????????????????xmlDoc.Save(Server.MapPath("People.xml"));
????????????????lblStatus.Text?=?"Data?successfully?added?to?XML?file.";
????????????????readXML();
????????????}
????????????catch
????????????{
????????????????lblStatus.Text?=?"Sorry,?unable?to?process?request.?Please?try?again.";
????????????}
????????}
????????protected?void?readXML()
????????{
????????????XDocument?xmlDoc?=?XDocument.Load(Server.MapPath("People.xml"));
????????????var?persons?=?from?person?in?xmlDoc.Descendants("Person")
??????????????????????????select?new
??????????????????????????{
??????????????????????????????Name?=?person.Element("Name").Value,
??????????????????????????????City?=?person.Element("City").Value,
??????????????????????????????Age?=?person.Element("Age").Value,
??????????????????????????};
????????????txtResults.Text?=?"";
????????????foreach?(var?person?in?persons)
????????????{
????????????????txtResults.Text?=?txtResults.Text?+?"Name:?"?+?person.Name?+?"\n";
????????????????txtResults.Text?=?txtResults.Text?+?"City:?"?+?person.City?+?"\n";
????????????????txtResults.Text?=?txtResults.Text?+?"Age:?"?+?person.Age?+?"\n\n";
????????????}
????????????if?(txtResults.Text?==?"")
????????????????txtResults.Text?=?"No?Results.";
????????}
????}
}
下載示例項目
?
原文:Using AJAX, LINQ and XML in C#
http://www.ajaxtutorials.com/ajax-tutorials/using-ajax-linq-and-xml-in-c/
?
另:實際上判斷字符串空可以用String.IsNullOrEmpty方法。
翻譯:Petter ?(版權歸原作者)
http://wintersun.cnblogs.com