Xml 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
显示不带格式的 XML 文档或使用可扩展样式表语言转换 (XSLT)。
public ref class Xml : System::Web::UI::Control
public class Xml : System.Web.UI.Control
type Xml = class
inherit Control
Public Class Xml
Inherits Control
- 继承
示例
下面的代码示例演示如何从示例 XML 文件和 XSL 转换样式表创建 XmlDocument 和 XslTransform 对象。 然后,XML 控件使用这些对象来显示 XML 文档。
<!--
The following example demonstrates how to create XmlDocument and
XslTransform objects from the sample XML and XSL Transform files.
The objects are then used by the Xml control to display the XML
document. Make sure the sample XML file is called People.xml and
the sample XSL Transform file is called Peopletable.xsl.
-->
<%@ Page Language="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
void Page_Load(Object sender, EventArgs e)
{
//<Snippet3>
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("people.xml"));
//</Snippet3>
//<Snippet4>
XslTransform trans = new XslTransform();
trans.Load(Server.MapPath("peopletable.xsl"));
//</Snippet4>
xml1.Document = doc;
xml1.Transform = trans;
}
</script>
<head runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
</form>
</body>
</html>
<!--
For this example to work, paste the following code into a file
named peopletable.xsl. Store the file in the same directory as
your .aspx file.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/People">
<xsl:apply-templates select="Person" />
</xsl:template>
<xsl:template match="Person">
<table width="100%" border="1">
<tr>
<td>
<b>
<xsl:value-of select="Name/FirstName" />
<xsl:value-of select="Name/LastName" />
</b>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="Address/Street" /><br />
<xsl:value-of select="Address/City" />
,
<xsl:value-of select="Address/State" />
<xsl:value-of select="Address/Zip" />
</td>
</tr>
<tr>
<td>
Job Title: <xsl:value-of select="Job/Title" /><br />
Description: <xsl:value-of select="Job/Description" />
</td>
</tr>
</table>
</xsl:template>
<xsl:template match="bookstore">
<bookstore>
<xsl:apply-templates select="book"/>
</bookstore>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:attribute name="ISBN">
<xsl:value-of select="@ISBN"/>
</xsl:attribute>
<price>
<xsl:value-of select="price"/>
</price>
<xsl:text>
</xsl:text>
</book>
</xsl:template>
</xsl:stylesheet>
-->
<!--
For this example to work, paste the following code into a file
named people.xml. Store the file in the same directory as
your .aspx file.
<?xml version="1.0" encoding="utf-8" ?>
<People>
<Person>
<Name>
<FirstName>Joe</FirstName>
<LastName>Suits</LastName>
</Name>
<Address>
<Street>1800 Success Way</Street>
<City>Redmond</City>
<State>WA</State>
<ZipCode>98052</ZipCode>
</Address>
<Job>
<Title>CEO</Title>
<Description>Wears the nice suit</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Linda</FirstName>
<LastName>Sue</LastName>
</Name>
<Address>
<Street>1302 American St.</Street>
<City>Paso Robles</City>
<State>CA</State>
<ZipCode>93447</ZipCode>
</Address>
<Job>
<Title>Attorney</Title>
<Description>Stands up for justice</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Jeremy</FirstName>
<LastName>Boards</LastName>
</Name>
<Address>
<Street>34 Palm Avenue</Street>
<City>Waikiki</City>
<State>HI</State>
<ZipCode>98052</ZipCode>
</Address>
<Job>
<Title>Pro Surfer</Title>
<Description>Rides the big waves</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Joan</FirstName>
<LastName>Page</LastName>
</Name>
<Address>
<Street>700 Webmaster Road</Street>
<City>Redmond</City>
<State>WA</State>
<ZipCode>98073</ZipCode>
</Address>
<Job>
<Title>Web Site Developer</Title>
<Description>Writes the pretty pages</Description>
</Job>
</Person>
</People>
-->
<!--
The following example demonstrates how to create XmlDocument and
XslTransform objects from the sample XML and XSL Transform files.
The objects are then used by the Xml control to display the XML
document. Make sure the sample XML file is called People.xml and
the sample XSL Transform file is called Peopletable.xsl.
-->
<%@ Page Language="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<script runat="server">
Sub Page_Load(sender As Object, e As EventArgs)
'<Snippet3>
Dim doc As XmlDocument = New XmlDocument()
doc.Load(Server.MapPath("people.xml"))
'</Snippet3>
'<Snippet4>
Dim trans As XslTransform = new XslTransform()
trans.Load(Server.MapPath("peopletable.xsl"))
'</Snippet4>
xml1.Document = doc
xml1.Transform = trans
End Sub
</script>
<head runat="server">
<title>Xml Class Example</title>
</head>
<body>
<h3>Xml Example</h3>
<form id="form1" runat="server">
<asp:Xml id="xml1" runat="server" />
</form>
</body>
</html>
<!--
For this example to work, paste the following code into a file
named peopletable.xsl. Store the file in the same directory as
your .aspx file.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/People">
<xsl:apply-templates select="Person" />
</xsl:template>
<xsl:template match="Person">
<table width="100%" border="1">
<tr>
<td>
<b>
<xsl:value-of select="Name/FirstName" />
<xsl:value-of select="Name/LastName" />
</b>
</td>
</tr>
<tr>
<td>
<xsl:value-of select="Address/Street" /><br />
<xsl:value-of select="Address/City" />
,
<xsl:value-of select="Address/State" />
<xsl:value-of select="Address/Zip" />
</td>
</tr>
<tr>
<td>
Job Title: <xsl:value-of select="Job/Title" /><br />
Description: <xsl:value-of select="Job/Description" />
</td>
</tr>
</table>
</xsl:template>
<xsl:template match="bookstore">
<bookstore>
<xsl:apply-templates select="book"/>
</bookstore>
</xsl:template>
<xsl:template match="book">
<book>
<xsl:attribute name="ISBN">
<xsl:value-of select="@ISBN"/>
</xsl:attribute>
<price>
<xsl:value-of select="price"/>
</price>
<xsl:text>
</xsl:text>
</book>
</xsl:template>
</xsl:stylesheet>
-->
<!--
For this example to work, paste the following code into a file
named people.xml. Store the file in the same directory as
your .aspx file.
<?xml version="1.0" encoding="utf-8" ?>
<People>
<Person>
<Name>
<FirstName>Joe</FirstName>
<LastName>Suits</LastName>
</Name>
<Address>
<Street>1800 Success Way</Street>
<City>Redmond</City>
<State>WA</State>
<ZipCode>98052</ZipCode>
</Address>
<Job>
<Title>CEO</Title>
<Description>Wears the nice suit</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Linda</FirstName>
<LastName>Sue</LastName>
</Name>
<Address>
<Street>1302 American St.</Street>
<City>Paso Robles</City>
<State>CA</State>
<ZipCode>93447</ZipCode>
</Address>
<Job>
<Title>Attorney</Title>
<Description>Stands up for justice</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Jeremy</FirstName>
<LastName>Boards</LastName>
</Name>
<Address>
<Street>34 Palm Avenue</Street>
<City>Waikiki</City>
<State>HI</State>
<ZipCode>98052</ZipCode>
</Address>
<Job>
<Title>Pro Surfer</Title>
<Description>Rides the big waves</Description>
</Job>
</Person>
<Person>
<Name>
<FirstName>Joan</FirstName>
<LastName>Page</LastName>
</Name>
<Address>
<Street>700 Webmaster Road</Street>
<City>Redmond</City>
<State>WA</State>
<ZipCode>98073</ZipCode>
</Address>
<Job>
<Title>Web Site Developer</Title>
<Description>Writes the pretty pages</Description>
</Job>
</Person>
</People>
-->
注解
在本主题中:
介绍
使用 Xml 控件在不设置格式或使用 XSL 转换的情况下显示 XML 文档的内容。
指定 XML 数据
要显示的 XML 文档是通过设置三个属性之一来指定的。 这三个属性表示可以显示的不同类型的 XML 文档。 可以通过设置相应的属性来显示 XML System.Xml.XmlDocument字符串或 XML 文件。 下表列出了用于指定 XML 文档的属性。
| 财产 | 说明 |
|---|---|
| Document | 使用 System.Xml.XmlDocument 对象设置 XML 文档。 警告: 此属性已过时。 使用本节中列出的其他属性之一设置控件的 Xml XML 内容。 |
| DocumentContent | 使用字符串设置 XML 文档。
注意:此属性通常以声明方式设置,方法是在控件的Xml开始标记和结束<asp:Xml>标记之间放置文本。 |
| DocumentSource | 使用文件设置 XML 文档。 |
注释
必须至少设置一个 XML 文档属性才能显示 XML 文档。 如果设置了多个 XML 文档属性,则会显示上一个属性集中引用的 XML 文档。 忽略其他属性中的文档。
指定 XSL 转换
可以选择指定一个 XSL 转换 (XSLT) 样式表,该样式表在将 XML 文档写入输出流之前设置两个属性之一来格式化该文档。 这两个属性表示可用于设置 XML 文档格式的不同类型的 XSL 转换样式表。 可以通过设置相应的属性,使用 System.Xml.Xsl.XslCompiledTransform 对象或 XSL 转换样式表文件设置 XML 文档的格式。 如果未指定 XSL 转换样式表,则使用默认格式显示 XML 文档。 下表列出了用于指定 XSL 转换样式表的属性。
| 财产 | 说明 |
|---|---|
| Transform | 使用指定的 System.Xml.Xsl.XslTransform 对象设置 XML 文档的格式。
注意:System.Xml.Xsl.XslTransform使用对象需要Full Trust权限。 |
| TransformSource | 使用指定的 XSL 转换样式表文件设置 XML 文档的格式。 |
注释
XSL 转换样式表是可选的。 无需设置 Transform 或属性 TransformSource 。 如果设置了这两个 XSL 转换样式表属性,则最后一个属性集确定使用哪个 XSL 转换样式表来设置 XML 文档的格式。 忽略另一个属性。
该 Xml 类还提供属性 TransformArgumentList ,使你可以提供具有可选参数的 XSL 转换样式表。 参数可以是 XSL 转换(XSLT)参数或扩展对象。
声明性语法
<asp:Xml
DocumentSource="uri"
EnableTheming="True|False"
EnableViewState="True|False"
ID="string"
OnDataBinding="DataBinding event handler"
OnDisposed="Disposed event handler"
OnInit="Init event handler"
OnLoad="Load event handler"
OnPreRender="PreRender event handler"
OnUnload="Unload event handler"
runat="server"
SkinID="string"
TransformSource="string"
Visible="True|False"
/>
构造函数
| 名称 | 说明 |
|---|---|
| Xml() |
初始化 Xml 类的新实例。 |
属性
| 名称 | 说明 |
|---|---|
| Adapter |
获取控件的特定于浏览器的适配器。 (继承自 Control) |
| AppRelativeTemplateSourceDirectory |
获取或设置包含此控件的 Page 或 UserControl 对象的应用程序相对虚拟目录。 (继承自 Control) |
| BindingContainer |
获取包含此控件的数据绑定的控件。 (继承自 Control) |
| ChildControlsCreated |
获取一个值,该值指示是否已创建服务器控件的子控件。 (继承自 Control) |
| ClientID |
ClientID重写属性并返回基本服务器控件标识符。 |
| ClientID |
获取由 ASP.NET 生成的 HTML 标记的控件 ID。 (继承自 Control) |
| ClientIDMode |
获取或设置用于生成属性值的 ClientID 算法。 (继承自 Control) |
| ClientIDSeparator |
获取一个表示属性中使用的 ClientID 分隔符的字符值。 (继承自 Control) |
| Context |
HttpContext获取与当前 Web 请求的服务器控件关联的对象。 (继承自 Control) |
| Controls |
Controls重写属性并返回基ControlCollection集合。 |
| Controls |
获取一个 ControlCollection 对象,该对象代表 UI 层次结构中指定服务器控件的子控件。 (继承自 Control) |
| DataItemContainer |
获取对命名容器的引用(如果命名容器实现 IDataItemContainer)。 (继承自 Control) |
| DataKeysContainer |
获取对命名容器的引用(如果命名容器实现 IDataKeysControl)。 (继承自 Control) |
| DesignMode |
获取一个值,该值指示控件是否在设计图面上使用。 (继承自 Control) |
| Document |
已过时.
获取或设置 XmlDocument 控件 Xml 中显示的值。 |
| DocumentContent |
设置一个字符串,其中包含要显示在控件中的 Xml XML 文档。 |
| DocumentSource |
获取或设置要在控件中显示的 Xml XML 文档的路径。 |
| EnableTheming |
EnableTheming重写属性。 类不支持 Xml 此属性。 |
| EnableViewState |
获取或设置一个值,该值指示服务器控件是否将视图状态及其包含的任何子控件的视图状态保存到请求客户端。 (继承自 Control) |
| Events |
获取控件的事件处理程序委托的列表。 此属性为只读。 (继承自 Control) |
| HasChildViewState |
获取一个值,该值指示当前服务器控件的子控件是否具有保存的视图状态设置。 (继承自 Control) |
| ID |
获取或设置分配给服务器控件的编程标识符。 (继承自 Control) |
| IdSeparator |
获取用于分隔控件标识符的字符。 (继承自 Control) |
| IsChildControlStateCleared |
获取一个值,该值指示此控件中包含的控件是否具有控件状态。 (继承自 Control) |
| IsTrackingViewState |
获取一个值,该值指示服务器控件是否正在保存对其视图状态的更改。 (继承自 Control) |
| IsViewStateEnabled |
获取一个值,该值指示是否为此控件启用视图状态。 (继承自 Control) |
| LoadViewStateByID |
获取一个值,该值指示控件是否参与加载其视图状态,而不是 ID 索引。 (继承自 Control) |
| NamingContainer |
获取对服务器控件命名容器的引用,该容器创建唯一的命名空间,用于区分具有相同 ID 属性值的服务器控件。 (继承自 Control) |
| Page |
获取对 Page 包含服务器控件的实例的引用。 (继承自 Control) |
| Parent |
获取对页面控件层次结构中服务器控件的父控件的引用。 (继承自 Control) |
| RenderingCompatibility |
获取一个值,该值指定呈现的 HTML 将与 ASP.NET 版本兼容。 (继承自 Control) |
| Site |
获取有关在设计图面上呈现时承载当前控件的容器的信息。 (继承自 Control) |
| SkinID | |
| TemplateControl |
获取或设置对包含此控件的模板的引用。 (继承自 Control) |
| TemplateSourceDirectory |
获取包含当前服务器控件的 Page 虚拟 UserControl 目录。 (继承自 Control) |
| Transform |
获取或设置 XslTransform 在将 XML 文档写入输出流之前格式化的对象。 |
| TransformArgumentList |
获取或设置一个 XsltArgumentList 包含传递给样式表并在可扩展样式表语言转换(XSLT)期间使用的可选参数的列表。 |
| TransformSource |
获取或设置可扩展样式表语言转换 (XSLT) 样式表的路径,该样式表在将 XML 文档写入输出流之前设置格式。 |
| UniqueID |
获取服务器控件的唯一分层限定标识符。 (继承自 Control) |
| ValidateRequestMode |
获取或设置一个值,该值指示控件是否检查浏览器的客户端输入是否存在潜在危险值。 (继承自 Control) |
| ViewState |
获取状态信息的字典,该字典允许跨同一页的多个请求保存和还原服务器控件的视图状态。 (继承自 Control) |
| ViewStateIgnoresCase |
获取一个值,该值指示对象是否 StateBag 不区分大小写。 (继承自 Control) |
| ViewStateMode |
获取或设置此控件的视图状态模式。 (继承自 Control) |
| Visible |
获取或设置一个值,该值指示服务器控件是否呈现为页面上的 UI。 (继承自 Control) |
| XPathNavigator |
获取或设置用于导航和编辑与 Xml 控件关联的 XML 数据的游标模型。 |
方法
活动
| 名称 | 说明 |
|---|---|
| DataBinding |
当服务器控件绑定到数据源时发生。 (继承自 Control) |
| Disposed |
在从内存中释放服务器控件时发生,这是请求 ASP.NET 页时服务器控件生命周期的最后阶段。 (继承自 Control) |
| Init |
在初始化服务器控件时发生,这是其生命周期中的第一步。 (继承自 Control) |
| Load |
在将服务器控件加载到对象中 Page 时发生。 (继承自 Control) |
| PreRender |
在 Control 加载对象但在呈现之前发生。 (继承自 Control) |
| Unload |
从内存中卸载服务器控件时发生。 (继承自 Control) |
显式接口实现
| 名称 | 说明 |
|---|---|
| IControlBuilderAccessor.ControlBuilder |
有关此成员的说明,请参阅 ControlBuilder。 (继承自 Control) |
| IControlDesignerAccessor.GetDesignModeState() |
有关此成员的说明,请参阅 GetDesignModeState()。 (继承自 Control) |
| IControlDesignerAccessor.SetDesignModeState(IDictionary) |
有关此成员的说明,请参阅 SetDesignModeState(IDictionary)。 (继承自 Control) |
| IControlDesignerAccessor.SetOwnerControl(Control) |
有关此成员的说明,请参阅 SetOwnerControl(Control)。 (继承自 Control) |
| IControlDesignerAccessor.UserData |
有关此成员的说明,请参阅 UserData。 (继承自 Control) |
| IDataBindingsAccessor.DataBindings |
有关此成员的说明,请参阅 DataBindings。 (继承自 Control) |
| IDataBindingsAccessor.HasDataBindings |
有关此成员的说明,请参阅 HasDataBindings。 (继承自 Control) |
| IExpressionsAccessor.Expressions |
有关此成员的说明,请参阅 Expressions。 (继承自 Control) |
| IExpressionsAccessor.HasExpressions |
有关此成员的说明,请参阅 HasExpressions。 (继承自 Control) |
| IParserAccessor.AddParsedSubObject(Object) |
有关此成员的说明,请参阅 AddParsedSubObject(Object)。 (继承自 Control) |
扩展方法
| 名称 | 说明 |
|---|---|
| FindDataSourceControl(Control) |
返回与指定控件的数据控件关联的数据源。 |
| FindFieldTemplate(Control, String) |
返回指定控件命名容器中指定列的字段模板。 |
| FindMetaTable(Control) |
返回包含数据控件的元表对象。 |