您的位置:首页 > 其它

XML学习笔记(四):格式化输出XML文档

2009-05-09 16:56 375 查看
页面代码:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="WriteXMLFormat.aspx.cs"Inherits="WriteXML"%>
<!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<htmlxmlns="http://www.w3.org/1999/xhtml">
<headrunat="server">
<title></title>
</head>
<body>
<formid="form1"runat="server">
<div>
ConnecionString:
<asp:TextBoxID="TextBox1"runat="server"Height="70px"Width="531px"></asp:TextBox><br/>
TableName:
<asp:TextBoxID="TextBox2"runat="server"Height="48px"Width="525px"></asp:TextBox><br/>
DestinationFileName:
<asp:TextBoxID="TextBox3"runat="server"Height="56px"Width="519px"></asp:TextBox><br/>
</div>
<asp:RadioButtonID="RadioButton1"Text="Columnsaselements"runat="server"GroupName="ExportType"/>
<br/>
<asp:RadioButtonID="RadioButton2"Text="Columnsasattributes"runat="server"GroupName="ExportType"/>
<br/>
<asp:CheckBoxID="CheckBox1"Text="FormatXMLDocument"runat="server"/>
<br/>
Indention:
<asp:TextBoxID="TextBox4"runat="server"></asp:TextBox>
<br/>
IndentCharacter:<br/>
<br/>
<asp:RadioButtonID="RadioButton3"Text="Space"GroupName="IndentCharacter"runat="server"/>
<br/>
<asp:RadioButtonID="RadioButton4"Text="Tab"GroupName="IndentCharacter"runat="server"/>
<br/>
<asp:ButtonID="Button1"runat="server"Height="31px"Text="ExportData"Width="134px"
OnClick="Button1_Click"/>
</form>
</body>
</html>

.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

执行代码:

usingSystem;
usingSystem.Data.SqlClient;
usingSystem.Xml;
publicpartialclassWriteXML:System.Web.UI.Page
{
protectedvoidPage_Load(objectsender,EventArgse)
{
}
///<summary>
///HandlestheClickeventoftheButton1control.
///</summary>
///<paramname="sender">Thesourceoftheevent.</param>
///<paramname="e">The<seecref="System.EventArgs"/>instancecontainingtheeventdata.</param>
protectedvoidButton1_Click(objectsender,EventArgse)
{
SqlConnectioncnn=newSqlConnection(TextBox1.Text);
SqlCommandcmd=newSqlCommand();
cmd.Connection=cnn;
cmd.CommandText="SELECT*FROM"+TextBox2.Text;
cnn.Open();
SqlDataReaderreader=cmd.ExecuteReader();
XmlTextWriterwriter=newXmlTextWriter(TextBox3.Text,null);
if(CheckBox1.Checked)
{
writer.Formatting=Formatting.Indented;
writer.Indentation=int.Parse(TextBox4.Text);
writer.IndentChar=(RadioButton3.Checked?'':'\t');
}
writer.WriteStartDocument();
writer.WriteComment("Fileexportedon"+DateTime.Now);
writer.WriteStartElement("table");
while(reader.Read())
{
if(RadioButton1.Checked)
{
writer.WriteStartElement("row");
for(inti=0;i<reader.FieldCount;i++)
{
writer.WriteStartElement(reader.GetName(i));
writer.WriteString(reader.GetValue(i).ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
}
else
{
writer.WriteStartElement("row");
for(inti=0;i<reader.FieldCount;i++)
{
writer.WriteAttributeString(reader.GetName(i),
reader.GetValue(i).ToString());
}
writer.WriteEndElement();
}
}
writer.WriteEndElement();
writer.Close();
reader.Close();
cnn.Close();
}
}


.csharpcode,.csharpcodepre
{
font-size:small;
color:black;
font-family:consolas,"CourierNew",courier,monospace;
background-color:#ffffff;
/*white-space:pre;*/
}
.csharpcodepre{margin:0em;}
.csharpcode.rem{color:#008000;}
.csharpcode.kwrd{color:#0000ff;}
.csharpcode.str{color:#006080;}
.csharpcode.op{color:#0000c0;}
.csharpcode.preproc{color:#cc6633;}
.csharpcode.asp{background-color:#ffff00;}
.csharpcode.html{color:#800000;}
.csharpcode.attr{color:#ff0000;}
.csharpcode.alt
{
background-color:#f4f4f4;
width:100%;
margin:0em;
}
.csharpcode.lnum{color:#606060;}

输出结果和界面:





未经格式化的XML输出文件





经过格式化的XML输出文件





WriteStartElement()方法的重载方法可以写入命名空间、前缀和LocalName



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: