ASP.Net/XML/XML File Save
Содержание
Load XML from String and write to file
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
XmlDocument empDoc = new XmlDocument();
Response.ContentType = "text/xml";
try
{
//Load the XML from a String
empDoc.LoadXml("<employees>" +
"<employee id="1">" +
"<name><firstName>First Name</firstName>" +
"<lastName>Last Name</lastName>" +
"</name><city>City</city>" +
"<state>WA</state><zipCode>99999</zipCode>" +
"</employee></employees>");
//Save the XML data onto a file
empDoc.Save(MapPath("EmployeesNew.xml"));
Response.Write(empDoc.InnerXml);
}
catch (XmlException xmlEx)
{
Response.Write("XmlException: " + xmlEx.Message);
}
catch (Exception ex)
{
Response.Write("Exception: " + ex.Message);
}
}
</script>
Save updated XML data set to new XML file
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<script language="VB" runat="server">
Sub Page_Load(Sender As Object, E As EventArgs)
Dim objDataSet As New DataSet()
" read in the XML file
objDataSet.ReadXml(Server.MapPath("NewEmployees.xml"))
" show it in a grid
dgEmployees1.DataSource = objDataSet.Tables(0).DefaultView
dgEmployees1.DataBind()
" modify a row
objDataSet.Tables("employee").Rows(0).Item("firstName") = "Bob"
objDataSet.Tables("employee").Rows(0).Item("lastName") = "Dylan"
" add a new row to the table
Dim objTable As DataTable
Dim objNewRow As DataRow
objTable = objDataSet.Tables("employee")
objNewRow = objTable.NewRow()
objNewRow.Item("firstName") = "Norman"
objNewRow.Item("lastName") = "Blake"
objTable.Rows.Add(objNewRow)
" save it to a new file
objDataSet.WriteXml(Server.MapPath("Employees2.xml"))
" read in the new file
Dim objDataSet2 As New DataSet()
objDataSet2.ReadXml(Server.MapPath("Employees2.xml"))
" show it in another grid
dgEmployees2.DataSource = objDataSet2.Tables(0).DefaultView
dgEmployees2.DataBind()
End Sub
</script>
<html>
<body>
<table>
<tr>
<td valign="top"><asp:DataGrid id="dgEmployees1" runat="server" /></td>
<td valign="top"><asp:DataGrid id="dgEmployees2" runat="server" /></td>
</tr>
</table>
</body>
</html>
<%--
<?xml version="1.0"?>
<employees>
<employee id="1">
<firstName>Nancy</firstName>
<lastName>Lee</lastName>
<city>Seattle</city>
<state>WA</state>
<zipCode>98122</zipCode>
</employee>
<employee id="2">
<firstName>Jason</firstName>
<lastName>Wang</lastName>
<city>Vancouver</city>
<state>WA</state>
<zipCode>98123</zipCode>
</employee>
</employees>
--%>
Save XML data to String Writer
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
XmlDocument empDoc = new XmlDocument();
try
{
StringWriter writer = new StringWriter();
//Load the XML from a String
empDoc.LoadXml("<?xml version="1.0"?><employees>" +
"<employee id="1">" +
"<firstName>first name</firstName>" +
"<lastName>last name</lastName>" +
"<city>city</city>" +
"<state>state</state><zipCode>99999</zipCode>" +
"</employee></employees>");
//Save the XML data onto a file
empDoc.Save(writer);
txtResult.Text = writer.ToString();
Response.Write(writer.ToString());
}
catch (XmlException xmlEx)
{
Response.Write("XmlException: " + xmlEx.Message);
}
catch (Exception ex)
{
Response.Write("Exception: " + ex.Message);
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Saving an XML Document to a StringWriter Object</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtResult" runat="server" Width="383px" Height="224px"></asp:TextBox>
</div>
</form>
</body>
</html>
Write XML File with namespace
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = MapPath("EmployeesNew.xml");
//string xmlFilePath = @"C:\Data\Employees.xml";
try
{
using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
{
writer.WriteStartDocument(false);
writer.WriteStartElement("employees");
//Write the Namespace prefix for the root element
writer.WriteAttributeString("xmlns", "emp", null, "urn:employees-nfex");
writer.WriteStartElement("employee", "urn:employees-nfex");
/* You can also use this approach to declare the namespace
string prefix = writer.LookupPrefix("urn:employees-nfex");
writer.WriteStartElement(prefix, "employee", null);
*/
writer.WriteAttributeString("id", "1");
writer.WriteStartElement("name", "urn:employees-nfex");
writer.WriteElementString("firstName", "urn:employees-nfex", "Nancy");
writer.WriteElementString("lastName", "urn:employees-nfex", "lastName");
writer.WriteEndElement();
writer.WriteElementString("city", "urn:employees-nfex", "Seattle");
writer.WriteElementString("state", "urn:employees-nfex", "WA");
writer.WriteElementString("zipCode", "urn:employees-nfex", "98122");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
//Flush the object and write the XML data to the file
writer.Flush();
lblResult.Text = "File is written successfully";
}
}
catch (Exception ex)
{
lblResult.Text = "An Exception occurred: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Writing XML File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="lblResult" runat="server" />
</div>
</form>
</body>
</html>
XML File write: element, comments
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
//string xmlFilePath = @"C:\Data\Employees.xml";
string xmlFilePath = MapPath("EmployeesNew.xml");
try {
using (XmlWriter writer = XmlWriter.Create(xmlFilePath))
{
writer.WriteStartDocument(false);
writer.WriteComment("This XML file represents the details of an employee");
//Start with the root element
writer.WriteStartElement("employees");
writer.WriteStartElement("employee");
writer.WriteAttributeString("id", "1");
writer.WriteStartElement("name");
writer.WriteElementString("firstName", "Nancy");
writer.WriteElementString("lastName", "Lee");
writer.WriteEndElement();
writer.WriteElementString("city", "Seattle");
writer.WriteElementString("state", "WA");
writer.WriteElementString("zipCode", "98122");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
//flush the object and write the XML data to the file
writer.Flush();
lblResult.Text = "File is written successfully";
}
}
catch (Exception ex)
{
lblResult.Text = "An Exception occurred: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Writing XML File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="lblResult" runat="server" />
</div>
</form>
</body>
</html>
XML file write: Indent, indent char, and Omit Xml Declaration
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(object sender, EventArgs e)
{
string xmlFilePath = MapPath("EmployeesNew.xml");
//string xmlFilePath = @"C:\Data\Employees.xml";
try
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.ConformanceLevel = ConformanceLevel.Auto;
settings.IndentChars = "\t";
settings.OmitXmlDeclaration = false;
using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
{
writer.WriteStartDocument(false);
writer.WriteStartElement("employees");
writer.WriteStartElement("employee");
writer.WriteAttributeString("id", "1");
writer.WriteStartElement("name");
writer.WriteElementString("firstName", "Nancy");
writer.WriteElementString("lastName", "Lee");
writer.WriteEndElement();
writer.WriteElementString("city", "Seattle");
writer.WriteElementString("state", "WA");
writer.WriteElementString("zipCode", "98122");
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
//Flush the object and write the XML data to the file
writer.Flush();
lblResult.Text = "File is written successfully";
}
}
catch (Exception ex)
{
lblResult.Text = "An Exception occurred: " + ex.Message;
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Writing XML File with XmlWriterSettings</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="lblResult" runat="server" />
</div>
</form>
</body>
</html>