📄 xml-to-dataset.aspx
字号:
<%@Page Language="C#"%>
<%@Import Namespace="System.Data" %>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.XPath" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Synchronization of the XMLDataDocument and DataSet objects</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Synchronization of the XMLDataDocument and DataSet objects</span><hr />
<!--------------------------------------------------------------------------->
<div id="outDocURL" runat="server"></div><p />
<b>List of authors found using the DOM</b>:<br/>
<div id="outDOMResult" runat="server"></div><p />
<b>List of authors found with an XPathNavigator</b>:<br/>
<div id="outXPNavResult" runat="server"></div><p />
<b>Contents of DataSet.Tables(0)</b>:<br/>
<asp:datagrid id="dgrResult" runat="server" /><p />
<b>XML from DataSet extracted with GetElementFromRow</b>:<br/>
<div id="outFromRowResult" 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("\\")) + "\\booklist-dataset.xml";
string strSchemaPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\booklist-schema.xsd";
// create a new XMLDataDocument object
XmlDataDocument objXMLDataDoc = new XmlDataDocument();
try
{
// load the XML schema into the XMLDataDocument object
objXMLDataDoc.DataSet.ReadXmlSchema(strSchemaPath);
outDocURL.InnerHtml = "Loaded file: <b><a href='" + strSchemaPath
+ "'>" + strSchemaPath + "</a></b><br />";
// load the XML file into the XMLDataDocument object
objXMLDataDoc.Load(strXMLPath);
outDocURL.InnerHtml += "Loaded file: <b><a href='" + strXMLPath
+ "'>" + strXMLPath + "</a></b><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 XMLDataDocument object
// *************************************************************
// *** 1: Extract the author names using the XML DOM methods ***
// *************************************************************
string strResults = "";
// create a NodeList collection of all matching child nodes
XmlNodeList colElements;
colElements = objXMLDataDoc.GetElementsByTagName("LastName");
int Dummy = colElements.Count; // **********************<<<<<<<< error if removed
// iterate through the collection getting the values of the
// child #text nodes for each one
foreach (XmlNode objNode in colElements)
strResults += objNode.FirstChild.Value + " ";
// then display the result
outDOMResult.InnerHtml = strResults;
strResults = "";
// *****************************************************************
// *** 2: extract the author names with an XPathNavigator object ***
// *****************************************************************
// create a new XPathNavigator object using the XMLDataDocument object
XPathNavigator objXPNav;
objXPNav = objXMLDataDoc.CreateNavigator();
// move to the root element of the document
objXPNav.MoveToRoot();
// and display the result of the recursive 'search' function
outXPNavResult.InnerHtml = SearchForElement(objXPNav, "LastName");
// ***************************************************************************
// *** 3: Display content of the XMLDataDocument object's DataSet property ***
// ***************************************************************************
// create a DataView object for the Books table in the DataSet
DataView objDataView = new DataView(objXMLDataDoc.DataSet.Tables[0]);
// assign the DataView object to the DataGrid control
dgrResult.DataSource = objDataView;
dgrResult.DataBind(); //and bind (display) the data
// *************************************************************************
// *** 4: Extract XML elements from the XMLDataDocument object's DataSet ***
// *************************************************************************
// create a DataTable object for the Books table in the DataSet
DataTable objDataTable = objXMLDataDoc.DataSet.Tables[0];
XmlElement objXMLElement;
// iterate through all the rows in this table
foreach (DataRow objRow in objDataTable.Rows)
{
// get an XML element that represents this row
objXMLElement = objXMLDataDoc.GetElementFromRow(objRow);
// HTMLEncode it because it contains XML element tags
strResults += Server.HtmlEncode(objXMLElement.OuterXml) + "<br />";
}
// display the result
outFromRowResult.InnerHtml = strResults;
// *************************************************************************
}
// recursive function used by step 2 - using an XPathNavigator
string SearchForElement(XPathNavigator objXPNav, string strNodeName)
{
string strResults = ""; // to hold the result
do //start a 'repeat-until' loop
{
// see if this node is an element that we're looking for
if (objXPNav.Name == strNodeName)
{
// move to the the first child (i.e the #text) node
objXPNav.MoveToFirstChild();
// get the value and add to the 'results' string
strResults += objXPNav.Value + " ";
// move back to the parent of this node (i.e. the element node)
objXPNav.MoveToParent();
}
// now check of the current node has any child nodes
if (objXPNav.HasChildren)
{
// move to the the first child node
objXPNav.MoveToFirstChild();
// recursively call this same function to search the child nodes
strResults += SearchForElement(objXPNav, strNodeName);
// move back to the parent of this node
objXPNav.MoveToParent();
}
}
// then move to next sibling of current node (if any)
while (objXPNav.MoveToNext());
return strResults; // return the result
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -