XmlDataSource.Save 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
如果DataFile设置了属性,则XmlDataSource控件当前保存在内存中的 XML 数据保存到磁盘。
public:
void Save();
public void Save();
member this.Save : unit -> unit
Public Sub Save ()
例外
对为 DataFile 属性指定的路径拒绝访问。
示例
本部分包含两个代码示例。 第一个代码示例演示如何对控件使用 XmlDataSource 控件 TreeView 来显示和编辑 XML 文件中包含的 XML 数据。 第二个 XmlDataSource 代码示例演示如何将控件与模板化 Repeater 控件一起使用,以显示和编辑 XML 文件中包含的 XML 数据。
下面的代码示例演示如何使用 XmlDataSource 控件和 TreeView 控件来显示和编辑 XML 文件中包含的 XML 数据。 每次选择TreeView节点时,都会使用GetXmlDocument该方法在内存中操作数据,然后将其保存到 XML 文件中。 最后, DataBind 在控件上 TreeView 调用以刷新它显示的数据。
<%@ Page LANGUAGE="C#" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" >
void TreeView1_SelectedNodeChanged(Object sender, EventArgs e)
{
XmlDocument myXml = new XmlDocument();
myXml=(XmlDocument)XmlSource.GetXmlDocument();
String iterator = TreeView1.SelectedNode.DataPath;
XmlNode myNode = myXml.SelectSingleNode(iterator);
myNode.InnerText = "ThisIsATest";
XmlSource.Save();
TreeView1.DataBind();
TreeView1.ExpandAll();
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:xmldatasource
runat="server"
id="XmlSource"
xpath="/bookstore/book"
datafile="Booksort.xml"
enableviewstate="False"
enablecaching="False" />
<asp:treeview
runat="server"
id="TreeView1"
ExpandDepth="3"
datasourceid="XmlSource"
maxdatabinddepth="3"
autogeneratedatabindings="False"
onselectednodechanged="TreeView1_SelectedNodeChanged" >
<databindings>
<asp:treenodebinding datamember="book" valuefield="publicationdate" />
<asp:treenodebinding datamember="title" valuefield="#InnerText" />
<asp:treenodebinding datamember="author" valuefield="#InnerText" />
<asp:treenodebinding datamember="first-name" valuefield="#InnerText" />
<asp:treenodebinding datamember="last-name" valuefield="#InnerText" />
</databindings>
</asp:treeview>
</form>
</body>
</html>
<%@ Page LANGUAGE="VB" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" >
Private Sub TreeView1_SelectedNodeChanged(sender As Object, e As EventArgs)
Dim myXml As New XmlDocument
myXml = CType(XmlSource.GetXmlDocument(), XmlDataDocument)
Dim iterator As String = TreeView1.SelectedNode.DataPath
Dim myNode As XmlNode = myXml.SelectSingleNode(iterator)
myNode.InnerText = "ThisIsATest"
XmlSource.Save()
TreeView1.DataBind()
TreeView1.ExpandAll()
End Sub ' TreeView1_SelectedNodeChanged
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server">
<asp:xmldatasource
runat="server"
id="XmlSource"
xpath="/bookstore/book"
datafile="Booksort.xml"
enableviewstate="False"
enablecaching="False" />
<asp:treeview
runat="server"
id="TreeView1"
ExpandDepth="3"
datasourceid="XmlSource"
maxdatabinddepth="3"
autogeneratedatabindings="False"
onselectednodechanged="TreeView1_SelectedNodeChanged" >
<databindings>
<asp:treenodebinding datamember="book" valuefield="publicationdate" />
<asp:treenodebinding datamember="title" valuefield="#InnerText" />
<asp:treenodebinding datamember="author" valuefield="#InnerText" />
<asp:treenodebinding datamember="first-name" valuefield="#InnerText" />
<asp:treenodebinding datamember="last-name" valuefield="#InnerText" />
</databindings>
</asp:treeview>
</form>
</body>
</html>
下面的代码示例演示如何将 XmlDataSource 控件与模板化 Repeater 控件一起使用,以显示和编辑 XML 文件中包含的 XML 数据。 与前面的示例一样,使用该方法检索GetXmlDocument的对象在内存XmlDataDocument中操作数据。 最后, DataBind 在控件上 TreeView 调用以刷新它显示的数据。
<%@ Page LANGUAGE="C#" SMARTNAVIGATION="false" %>
<%@ Import NameSpace="System.Xml" %>
<script runat="server" >
void Button1_Click(Object sender, EventArgs e)
{
XmlDocument myXml = new XmlDocument();
myXml=(XmlDocument)XmlSource.GetXmlDocument();
String path = "bookstore/book/@publicationdate";
XmlNodeList nodeList;
nodeList = myXml.SelectNodes(path);
foreach (XmlNode date in nodeList)
{
int helper = int.Parse(date.Value) + 2;
date.Value = helper.ToString();
}
XmlSource.Save();
Repeater1.DataBind();
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server" >
<asp:XmlDataSource
runat="server"
ID="XmlSource"
XPath="bookstore/book[@genre='novel']"
DataFile="Booksort2.xml"
EnableViewState="True"
EnableCaching="False" />
<asp:Repeater
runat="server"
ID="Repeater1"
DataSourceID="XmlSource" >
<ItemTemplate >
<h1><%# XPath ("title/text()") %> </h1>
<b>Author:</b><%# XPath ("author/first-name/text()") %> <%# XPath ("author/last-name/text()") %>
<b>PublicationDate:</b><%# XPath ("@publicationdate") %>
<b>Price:</b><%# XPath ("price/text()") %>
</ItemTemplate>
</asp:Repeater>
<p><asp:Button
runat="server"
ID="Button1"
onclick="Button1_Click"
Text="Add 2 years to the Publication Date!" /></p>
</form>
</body>
</html>
<%@ Page LANGUAGE="VB" SMARTNAVIGATION="false" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server" >
Private Sub Button1_Click(sender As Object, e As EventArgs)
Dim myXml As New XmlDocument
myXml = CType(XmlSource.GetXmlDocument(), XmlDocument)
Dim path As String = "bookstore/book/@publicationdate"
Dim nodeList As XmlNodeList = myXml.SelectNodes(path)
Dim aDate As XmlNode
For Each aDate In nodeList
Dim helper As Integer = Int32.Parse(aDate.Value) + 2
aDate.Value = helper.ToString()
Next aDate
XmlSource.Save()
Repeater1.DataBind()
End Sub 'Button1_Click
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>ASP.NET Example</title>
</head>
<body>
<form id="Form1" runat="server" >
<asp:XmlDataSource
runat="server"
ID="XmlSource"
XPath="bookstore/book[@genre='novel']"
DataFile="Booksort2.xml"
EnableViewState="True"
EnableCaching="False" />
<asp:Repeater
runat="server"
ID="Repeater1"
DataSourceID="XmlSource" >
<ItemTemplate >
<h1><%# XPath ("title/text()") %> </h1>
<b>Author:</b><%# XPath ("author/first-name/text()") %> <%# XPath ("author/last-name/text()") %>
<b>PublicationDate:</b><%# XPath ("@publicationdate") %>
<b>Price:</b><%# XPath ("price/text()") %>
</ItemTemplate>
</asp:Repeater>
<p><asp:Button
runat="server"
ID="Button1"
onclick="Button1_Click"
Text="Add 2 years to the Publication Date!" /></p>
</form>
</body>
</html>
代码示例中的 XML 文件具有以下数据:
<?xml version="1.0" encoding="utf-8"?>
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1999" bk:ISBN="0000000000">
<title>Secrets of Silicon Valley</title>
<author>
<first-name>Sheryl</first-name>
<last-name>Hunter</last-name>
</author>
<price>24.95</price>"
</book>
<book genre="novel" publicationdate="1985" bk:ISBN="1111111111">
<title>Straight Talk About Computers</title>
<author>
<first-name>Dean</first-name>
<last-name>Straight</last-name>
</author>
<price>29.95</price>
</book>
</bookstore>
注解
虽然控件 XmlDataSource 通常用于只读数据绑定方案中,但可以使用该 XmlDataSource 控件编辑基础 XML 数据文件中的 XML 数据。 在这些情况下,XML 数据由 XmlDataSource 控件从 XML 文件加载。 使用该方法GetXmlDocument修改XmlDataDocument内存中,然后通过调用Save该方法保存到 XML 数据文件。 如果满足以下条件,则可能出现此可编辑的 XML 方案:
未在或TransformFile属性中Transform指定 XSLT 转换。
该方法 Save 不处理不同请求的并发保存操作。 如果多个用户通过 XmlDataSource 控件编辑 XML 文件,则不能保证所有用户都使用相同的数据运行。 操作也可能 Save 由于这些相同的并发问题而失败。