Xml 类

显示 XML 文档,不进行格式化或使用扩展样式表语言转换 (XSLT)。

**命名空间:**System.Web.UI.WebControls
**程序集:**System.Web(在 system.web.dll 中)

语法

声明
Public Class Xml
    Inherits Control
用法
Dim instance As Xml
public class Xml : Control
public ref class Xml : public Control
public class Xml extends Control
public class Xml extends Control

备注

使用 Xml 控件显示 XML 文档的内容,不进行格式化或使用 XSL 转换。

通过设置三个属性之一来指定要显示的 XML 文档。这三个属性表示可以显示的不同类型的 XML 文档。通过设置适当的属性,可以显示 System.Xml.XmlDocument、XML 字符串或 XML 文件。下表列出了用于指定 XML 文档的属性。

属性

说明

Document

System.Xml.XmlDocument 对象来设置 XML 文档。

DocumentContent

用字符串来设置 XML 文档。

提示

通常以声明方式,通过将文本放在 Xml 控件的开始和结束 <asp:Xml> 标记之间来设置此属性。

DocumentSource

用文件来设置 XML 文档。

提示

必须至少设置一个 XML 文档属性才能显示 XML 文档。如果设置了一个以上的 XML 文档属性,则显示最后设置的属性中引用的 XML 文档。其他属性中的文档被忽略。

通过设置两个属性之一,可以选择指定 XSL 转换 (XSLT) 样式表,该样式表在 XML 文档被写入输出流之前对其进行格式化。这两个属性表示可用于格式化 XML 文档的不同类型的 XSL 转换样式表。通过设置适当的属性,可以用 System.Xml.Xsl.XslCompiledTransform 对象或 XSL 转换样式表文件来格式化 XML 文档。如果未指定 XSL 转换样式表,则使用默认格式显示 XML 文档。下表列出了用于指定 XSL 转换样式表的属性。

属性

说明

Transform

用指定的 System.Xml.Xsl.XslTransform 对象格式化 XML 文档。

TransformSource

用指定的 XSL 转换样式表文件格式化 XML 文档。

提示

XSL 转换样式表是可选的。您不需要设置 TransformTransformSource 属性。如果同时设置两个 XSL 转换样式表属性,则最后设置的属性确定使用哪个 XSL 转换样式表格式化 XML 文档。另一个属性被忽略。

Xml 类还提供 TransformArgumentList 属性,该属性使您得以提供具有可选参数的 XSL 转换样式表。这些参数可以是 XSL 转换 (XSLT) 参数或扩展对象。

主题 位置
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中构建 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 XML Web 服务器控件 (Visual Studio) 在 Visual Studio 中构建 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 XML Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:将 XML 数据加载到 XML Web 服务器控件中 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:转换 XML Web 服务器控件中的 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 XML Web 服务器控件 (Visual Studio) 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:将 XML 数据加载到 XML Web 服务器控件中 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:创建网页以显示 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
演练:使用转换在 Web 窗体页中显示 XML 文档 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:转换 XML Web 服务器控件中的 XML 数据 在 Visual Studio 中生成 ASP .NET Web 应用程序
如何:向 Web 窗体页添加 XML Web 服务器控件 生成 ASP .NET Web 应用程序
如何:将 XML 数据加载到 XML Web 服务器控件中 生成 ASP .NET Web 应用程序
如何:转换 XML Web 服务器控件中的 XML 数据 生成 ASP .NET Web 应用程序

示例

下面的代码示例说明如何用示例 XML 文件和 XSL 转换样式表创建 XmlDocumentXslTransform 对象。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="VB" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script runat="server">
      Sub Page_Load(sender As Object, e As EventArgs)
         Dim doc As XmlDocument = New XmlDocument()
         doc.Load(Server.MapPath("people.xml"))

         Dim trans As XslTransform = new XslTransform()
         trans.Load(Server.MapPath("peopletable.xsl"))

         xml1.Document = doc
         xml1.Transform = trans
      End Sub
</script>
<body>
   <h3>Xml Example</h3>
   <form 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.

<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" />
                  &#160;
                  <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">
      <!-- Prices and books -->
      <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.


<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="C#" AutoEventWireup="True" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.Xsl" %>
<html>
   <script runat="server">
      void Page_Load(Object sender, EventArgs e) 
      {
         XmlDocument doc = new XmlDocument();
         doc.Load(Server.MapPath("people.xml"));

         XslTransform trans = new XslTransform();
         trans.Load(Server.MapPath("peopletable.xsl"));

         xml1.Document = doc;
         xml1.Transform = trans;
      }
   </script>
<body>
   <h3>Xml Example</h3>
      <form 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.

<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" />
                  &#160;
                  <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">
      <!-- Prices and books -->
      <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.


<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>

-->

.NET Framework 安全性

继承层次结构

System.Object
   System.Web.UI.Control
    System.Web.UI.WebControls.Xml

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

Xml 成员
System.Web.UI.WebControls 命名空间
System.Xml.XmlDocument
System.Xml.Xsl.XslTransform
Document
DocumentContent
DocumentSource
TransformArgumentList

其他资源

XML Web 服务器控件