XML学习笔记(四):格式化输出XML文档
2009-05-09 16:56
375 查看
页面代码:
.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;}
执行代码:
.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
<%@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
相关文章推荐
- Object C学习笔记2-NSLog 格式化输出数据
- Object C学习笔记2-NSLog 格式化输出数据
- Java格式化输出学习笔记
- Delphi的学习笔记七——格式化输出函数(总结的很全面)
- Python学习笔记之格式化输出
- Python学习笔记2-Python神奇的语法和格式化输出
- Python3学习笔记【基础2--输出,输入,格式化,编码】
- Object C学习笔记2-NSLog 格式化输出数据
- Java学习笔记之Formatter的用法详解(输出格式化)
- PowerShell 学习笔记-- 3 -- 格式化输出,文件系统
- Python学习笔记2-Python神奇的语法和格式化输出
- [学习笔记][C++Primer Plus]使用cout格式化输出字符串,3q xuzhong
- perl学习笔记八----格式化输出
- 如何输出格式化的字符串(学习笔记四)
- [学习笔记][C++Primer Plus]使用cout格式化输出字符串
- Python学习笔记2-Python神奇的语法和格式化输出
- Delphi的学习笔记七——格式化输出函数(总结的很全面)
- perl学习笔记八----格式化输出
- gcc中格式化输出函数-学习笔记
- Delphi的学习笔记七——格式化输出函数(总结的很全面)