当数据库字段为varchar/char等字符串类型时,如果某字段为null此时保存到xml中的节点将不会显示,处理办法可以将保存xml的字段类型设置为image类型,具体参见示例。(记得在.aspx文件头中加入这句:<%@ Page validateRequest="false" %>)
1.html
<HTML>
<HEAD>
<title>StreamXml</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server"
TextMode="MultiLine" Height="160px" Width="352px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 184px; POSITION: absolute; TOP: 16px" runat="server"
Text="保存"></asp:Button>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 48px" runat="server"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
AutoGenerateColumns="False" OnItemCommand="Compare">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="UserID" HeaderText="UserID"></asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="UserName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="比较">
<ItemTemplate>
<asp:Button id="Button2" runat="server" Text="比较" CommandName="Compare"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
<asp:TextBox id="TextBox3" style="Z-INDEX: 105; LEFT: 240px; POSITION: absolute; TOP: 192px"
runat="server" TextMode="MultiLine" Height="144px" Width="352px"></asp:TextBox>
</form>
</body>
</HTML>
2.cs代码<HEAD>
<title>StreamXml</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:TextBox id="TextBox1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 16px" runat="server"></asp:TextBox>
<asp:TextBox id="TextBox2" style="Z-INDEX: 102; LEFT: 240px; POSITION: absolute; TOP: 16px" runat="server"
TextMode="MultiLine" Height="160px" Width="352px"></asp:TextBox>
<asp:Button id="Button1" style="Z-INDEX: 103; LEFT: 184px; POSITION: absolute; TOP: 16px" runat="server"
Text="保存"></asp:Button>
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 8px; POSITION: absolute; TOP: 48px" runat="server"
BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White" CellPadding="4"
AutoGenerateColumns="False" OnItemCommand="Compare">
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<Columns>
<asp:BoundColumn Visible="False" DataField="UserID" HeaderText="UserID"></asp:BoundColumn>
<asp:BoundColumn DataField="UserName" HeaderText="UserName"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="比较">
<ItemTemplate>
<asp:Button id="Button2" runat="server" Text="比较" CommandName="Compare"></asp:Button>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid>
<asp:TextBox id="TextBox3" style="Z-INDEX: 105; LEFT: 240px; POSITION: absolute; TOP: 192px"
runat="server" TextMode="MultiLine" Height="144px" Width="352px"></asp:TextBox>
</form>
</body>
</HTML>
public class StreamXml : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
BindData
SaveXml
SaveUserInfo
GetDataSet
Compare event
Web Form Designer generated code
save
property
}
3.数据库脚本{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
BindData
SaveXml
SaveUserInfo
GetDataSet
Compare event
Web Form Designer generated code
save
property
}
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StreamXml]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[StreamXml]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserInfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[UserInfo]
GO
CREATE TABLE [dbo].[StreamXml] (
[UserID] [uniqueidentifier] NULL ,
[XmlStream] [image] NULL ,
[XmlString] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[UserInfo] (
[UserID] [uniqueidentifier] NULL ,
[UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO
drop table [dbo].[StreamXml]
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[UserInfo]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [dbo].[UserInfo]
GO
CREATE TABLE [dbo].[StreamXml] (
[UserID] [uniqueidentifier] NULL ,
[XmlStream] [image] NULL ,
[XmlString] [varchar] (500) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE TABLE [dbo].[UserInfo] (
[UserID] [uniqueidentifier] NULL ,
[UserName] [varchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
GO