|
C#•Parse the XML Template Listing 2. Use .NET's XmlTextReader class to parse the XML template and locate specific elements. This portion of the code shows you how to create user controls dynamically and identify the ContentPlaceHolder defined in the template. //Locate template elements in the
//http://www.xmlforasp.net/templates
//namespace
if (reader.NamespaceURI.ToLower().Equals(uriText)) {
string id =
reader.GetAttribute("id");
if (reader.LocalName.ToLower().Equals(ContentPlaceHolderText)){
if (reader.NodeType ==
XmlNodeType.Element) {
PlaceHolder ph = new
PlaceHolder();
if (id != null) ph.ID = id;
foundContent = true;
if (root.Body.Form == null)
root.Body.Form = new
BaseForm();
root.Body.Form.Content =
ph;
AddControls(ph);
} else if (reader.NodeType ==
XmlNodeType.EndElement) {
currentControl =
currentControl.Parent;
}
} else if
(reader.LocalName.ToLower().Equals(UserControlText)){
try {
Control uc =
LoadControl(reader.GetAttribute("src"));
if (id != null) uc.ID = id;
currentControl.Controls.Add(uc);
}
catch (Exception exp) {
throw new
ApplicationException("Error
loading template " +
"user control.", exp);
}
}
}
}
|