xml-via-textwriter.aspx
来自「Professional ASP.NET source code」· ASPX 代码 · 共 112 行
ASPX
112 行
<%@Page Language="C#" %>
<%@Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Creating an Xml document with an XmlTextWriter object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Creating an Xml document with an XmlTextWriter object</span><hr />
<!--------------------------------------------------------------------------->
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server"> </div>
<div id="outResults" runat="server"></div>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// create physical path for the new file (in same folder as ASPX page)
string strCurrentPath = Request.PhysicalPath;
string strXmlPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\newbooklist.xml";
// declare a variable to hold an XmlTextWriter object
XmlTextWriter objXmlWriter;
try
{
// create a new objXmlWriter object for the Xml file
objXmlWriter = new XmlTextWriter(strXmlPath, null);
outDocURL.InnerHtml = "Writing to file: <b>" + strXmlPath + "</b>";
}
catch (Exception objError)
{
// display error details
outError.InnerHtml = "<b>* Error while accessing document</b>.<br />"
+ objError.Message + "<br />" + objError.Source;
return; // and stop execution
}
// now ready to write (or "push") the nodes for the new Xml document
// turn on indented formatting and set indent to 3 chararcters
objXmlWriter.Formatting = Formatting.Indented;
objXmlWriter.Indentation = 3;
// start the document with the Xml declaration tag
objXmlWriter.WriteStartDocument();
// write a comment element including the current date/time
objXmlWriter.WriteComment("Created using an XmlTextWriter - " + DateTime.Now);
// write the opening tag for the <BookList> root element
objXmlWriter.WriteStartElement("BookList");
// write the opening tag for a <Book> element
objXmlWriter.WriteStartElement("Book");
// add two attributes to this element's opening tag
objXmlWriter.WriteAttributeString("Category", "Technology");
int intPageCount = 1248; // numeric value to convert
objXmlWriter.WriteAttributeString("Pagecount", intPageCount.ToString("G"));
// write four elements, using different source data types
objXmlWriter.WriteElementString("Title", "Professional Video Recorder Programming");
DateTime datReleaseDate = new DateTime(2000, 03, 03);
objXmlWriter.WriteElementString("ReleaseDate", datReleaseDate.ToString("yyyy-MM-dd"));
int intSales = 17492;
objXmlWriter.WriteElementString("Sales", intSales.ToString("G"));
bool blnHardback = true;
objXmlWriter.WriteElementString("Hardback", blnHardback.ToString());
// write the opening tag for the <AuthorList> child element
objXmlWriter.WriteStartElement("AuthorList");
// add two <Author> elements
objXmlWriter.WriteElementString("Author", "Francesca Unix");
objXmlWriter.WriteElementString("Author", "William Soft");
// close the <AuthorList> element
objXmlWriter.WriteEndElement();
// close the <Book> element
objXmlWriter.WriteEndElement();
// close the root <BookList> element
objXmlWriter.WriteEndElement();
// flush the current content to the file and close it
objXmlWriter.Flush();
objXmlWriter.Close();
// now open the new Xml file and read it into a string
string strXmlResult;
StreamReader objSR = File.OpenText(strXmlPath);
strXmlResult = objSR.ReadToEnd();
objSR.Close();
// and display the results in the page
outResults.InnerHtml = "<pre>" + Server.HtmlEncode(strXmlResult) + "<pre>";
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?