[thelist] XML data storage
Nik Schramm
n at industriality.com
Wed May 15 06:54:01 CDT 2002
Chris,
>>If this is a sensible way to go, which is the best way to
>>search an XML
>>document for a particular username? I'm using ASP.NET...
save this page as whatever.aspx, fill in the path to your xml doc and
give it a try, it ought to help:
[code]
<%@ Page Language="C#" Debug="true" %>
<%@ Import Namespace="System.Xml" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title> title me</title>
<meta http-equiv="content-type" content="text/xhtml;
charset=iso-8859-1" />
<style type="text/css">
<!--
body { font: 12px Verdana, sans-serif; }
.label { color:#666; font-size:11px; }
//-->
</style>
<script language="C#" runat="server">
void CheckUser(Object sender, System.EventArgs
e) {
String user = txtUsername.Value;
XmlDocument xmlDoc = new XmlDocument();
// set the path to your xml doc here
xmlDoc.Load(Server.MapPath("test.xml"));
int count =
xmlDoc.DocumentElement.ChildNodes.Count;
bool found = false;
int foundAt = 0;
for (int i=0;i<count;i++) {
if((string)
xmlDoc.DocumentElement.ChildNodes[i].Attributes["name"].Value == user) {
found = true;
foundAt = i;
break;
}
}
// display results
if(found) {
divresult.InnerHtml = "<p>User "
+ user + " found at Child Node " + foundAt + "</p><p>";
int kids =
xmlDoc.DocumentElement.ChildNodes[foundAt].ChildNodes.Count;
for (int j=0;j<kids;j++) {
divresult.InnerHtml +=
"<span class='label'><" +
xmlDoc.DocumentElement.ChildNodes[foundAt].ChildNodes[j].Name +
"></span>"
+xmlDoc.DocumentElement.ChildNodes[foundAt].ChildNodes[j].InnerText +
"<span class='label'></" +
xmlDoc.DocumentElement.ChildNodes[foundAt].ChildNodes[j].Name +
"></span><br />";
}
} else {
divresult.InnerHtml = "<p>User "
+ user + " not found.";
}
divresult.InnerHtml += "</p>";
}
</script>
</head>
<body>
<form runat="server">
<p>Username: <input type="text" id="txtUsername"
size="30" runat="server" /><input type="submit" id="btnSubmit"
value="Find" runat="server" onserverclick="CheckUser" /></p>
</form>
<div id="divresult" runat="server" />
</body>
</html>
[/code]
/nik
www.industriality.com - candy for the inner eye
More information about the thelist
mailing list