edit-xml.aspx
来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 179 行
ASPX
179 行
<%@Page Language="C#"%>
<%@Import Namespace="System.Xml" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Creating and Editing the Content of XML Documents</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Creating and Editing the Content of XML Documents</span><hr />
<!--------------------------------------------------------------------------->
<div id="outDocURL" runat="server"></div><p />
<div id="outResult1" runat="server"></div><p />
<div id="outResult2" runat="server"></div><p />
<div id="outResult3" runat="server"></div><p />
<div id="outError" runat="server"></div>
<script language="C#" runat="server">
void Page_Load(Object sender, EventArgs e)
{
// create physical path to booklist sample files (in same folder as ASPX page)
string strCurrentPath = Request.PhysicalPath;
string strXMLPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\bookdetails.xml";
string strnewPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\newdetails.xml";
// create a new XMLDocument object
XmlDocument objXMLDoc = new XmlDocument();
try
{
// load the XML file into the XMLDocument object
objXMLDoc.Load(strXMLPath);
outDocURL.InnerHtml = "<b>* Loaded file</b>: <a href='" + strXMLPath
+ "'>" + strXMLPath + "</a><br />";
}
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 use the XMLDocument object
// *************************************************************
// *** 1: Select a Node, display content and then remove it ***
// *************************************************************
// specify XPath expression to select a book elment
string strXPath = "descendant::Book[ISBN='1861003234']";
// get a reference to the matching <Book> node
XmlNode objNode;
objNode = objXMLDoc.SelectSingleNode(strXPath);
// then display the result
outResult1.InnerHtml = "XPath expression '<b>" + strXPath
+ "</b>' returned:<br />"
+ Server.HtmlEncode(objNode.OuterXml) + "<br />";
// delete this node using RemoveChild method from document element
objXMLDoc.DocumentElement.RemoveChild(objNode);
outResult1.InnerHtml += "<b>* Removed node from document.</b><br />";
// ******************************************************************
// *** 2: Create empty XML document, add declaration and comment ***
// ******************************************************************
// create new empty XML Document object
XmlDocument objnewDoc = new XmlDocument();
// create a new XmlDeclaration object
XmlDeclaration objDeclare;
objDeclare = objnewDoc.CreateXmlDeclaration("1.0", null, null);
// and add it as the first node in the new document
objDeclare = (XmlDeclaration)objnewDoc.InsertBefore(objDeclare, objnewDoc.DocumentElement);
// create a new XmlComment object
XmlComment objComment;
objComment = objnewDoc.CreateComment("new document created " + DateTime.Now);
// and add it as the second node in the new document
objComment = (XmlComment)objnewDoc.InsertAfter(objComment, objDeclare);
// ********************************************************************
// *** 3: Select node in original document and import into new one ***
// ********************************************************************
// change the XPath expression to select a different book
strXPath = "descendant::Book[ISBN='1861003382']";
// get a reference to the matching <Book> node
objNode = objXMLDoc.SelectSingleNode(strXPath);
// create a variable to hold the imported node object
XmlNode objImportedNode;
// import the node and all children into new document (un-attached fragment)
objImportedNode = objnewDoc.ImportNode(objNode, true);
// insert the new un-attached node into document after the comment node
objnewDoc.InsertAfter(objImportedNode, objComment);
// display the contents of the new document
outResult2.InnerHtml = "Created new XML document and inserted "
+ "into it the node selected by<br />"
+ "the XPath expression '<b>" + strXPath + "'</b><br />"
+ "<b>* Content of new document is</b>:<br />"
+ Server.HtmlEncode(objnewDoc.OuterXml);
// *****************************************************************
// *** 4: Select and edit/insert new content into ISBN elements ***
// *****************************************************************
// change the XPath expression to select all ISBN elements
strXPath = "descendant::ISBN";
// get a reference to the matching nodes as a collection
XmlNodeList colNodeList;
colNodeList = objXMLDoc.SelectNodes(strXPath);
// display the number of matches found
outResult3.InnerHtml = "Found <b>" + colNodeList.Count + "</b> nodes matching the"
+ "XPath expression '<b>" + strXPath + "'</b><br />"
+ "<b>* Editing and inserting new content</b><br />";
string strNodeValue, strnewValue, strShortCode;
// create a variable to hold an XmlAttribute object
XmlAttribute objAttr;
// iterate through all the nodes found
foreach (XmlNode objNd in colNodeList)
{
// create an XmlAttribute named 'formatting'
objAttr = objXMLDoc.CreateAttribute("formatting");
// set the value of the XmlAttribute to 'hyphens'
objAttr.Value = "hyphens";
// and add it to this ISBN element - have to cast the object
// to an XmlElement as XmlNode doesn't have this method
((XmlElement)objNd).SetAttributeNode(objAttr);
// get text value of this ISBN element
strNodeValue = objNd.InnerText;
// create short and long strings to replace content
strShortCode = strNodeValue.Substring(strNodeValue.Length - 4);
strnewValue = strNodeValue.Substring(1, 1) + "-" + strNodeValue.Substring(2, 6)
+ "-" + strNodeValue.Substring(8, 2) + "-" + strNodeValue.Substring(strNodeValue.Length - 1, 1);
// insert into element by setting the InnerXml property
objNd.InnerXml = "<LongCode>" + strnewValue + "</LongCode><ShortCode>"
+ strShortCode + "</ShortCode>";
}
// write the updated document to a disk file
objXMLDoc.Save(strnewPath);
// display a link to view the updated document
outResult3.InnerHtml += "<br /><b>* Saved updated document</b>: <a href='"
+ strnewPath + "'>" + strnewPath + "</a><br />";
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?