幼儿园在线幼儿园网站幼儿园评论www.yeyweb.com免费幼儿园网站建设

asp.net2.0学习笔记XML部分

 

1、 XML InfoSet是一个W3C概念,它描述了XML文档中哪些事重要的,哪些是不重要的。

2、 XSD:相应的XML模式定义,它描述了文档中允许有哪些元素、简单类型和复杂类型。在编程语言中,XSDXML模式定义)是一个类(型)定义,而XML文档是该类型的一个实例。

3、 XPathDocument是只读游标,可以前后移动,使用方法:举一个小例子

        string bookfile = Server.MapPath("App_Data""Books.xml");

        XPathDocument document = new XPathDocument(bookfile);

        XPathNavigator nav = document.CreateNavigator();

        XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);

        namespaceMgr.AddNamespace("b", "http://www.inuli.cn");

        foreach (XPathNavigator node in nav.Select("//b:book[not(b:price[.>10.00])]/b:price",namespaceMgr))

        {

            decimal price = (decimal)node.ValueAs(typeof(decimal));

            Response.Write(string.Format("价格是:{0}<br/>",price));

        }

4、 XmlDocument不是只读的,可以修改内存中的数据,示例:

        string bookfile = Server.MapPath("App_Data""Books.xml");

        XmlDocument document = new XmlDocument();

        document.Load(bookfile);

        XPathNavigator nav = document.CreateNavigator();

        XmlNamespaceManager namespaceMgr = new XmlNamespaceManager(nav.NameTable);

        namespaceMgr.AddNamespace("b", "http://www.inuli.cn");

        foreach (XPathNavigator node in nav.Select("//b:book[not(b:price[.>10.00])]/b:price", namespaceMgr))

        {

            decimal price = (decimal)node.ValueAs(typeof(decimal));

            node.SetTypedValue(price * 1.2M);

            Response.Write(string.Format("价格从{0}涨到{1}<br/>", price,node.ValueAs(typeof(decimal))));

        }

5、 DataSet

使用DataSetSQL Server中提取XML

        string connStr = "database=Northwind;Data Source=.;User id=sa;pwd=123123";

        using (SqlConnection conn = new SqlConnection(connStr))

        {

            SqlCommand command = new SqlCommand("select * from Customers", conn);

            conn.Open();

            DataSet ds = new DataSet();

            ds.DataSetName = "Customers";

            ds.Load(command.ExecuteReader(), LoadOption.OverwriteChanges, "Customer");

            Response.ContentType = "text/xml";

            ds.WriteXml(Response.OutputStream);

        }

6、 XmlDataSource控件使用 示例:

    <div>

        <asp:DataList ID="DataList1" runat="server" DataSourceID="XmlDataSource1">

        <ItemTemplate>

                    <p>

                    <b>

                    <%#XPath("author/first-name") %>-<%#XPath("author/last-name") %>

                    </b>

                    worte<%#XPath("title") %>

                </p>

        </ItemTemplate>

        </asp:DataList></div>

        <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/App_Data/Books.xml"

            XPath="//bookstore/book"></asp:XmlDataSource>

7、 XSLT:适合于快速、简单地转换XML文件的结构。示例:(提取Books.xml,把它转换为一个包含一组作者的文件。)

<xsl:stylesheetversion="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:templatematch="/">

 <xsl:elementname="Authors">

    <xsl:apply-templatesselect="//book"></xsl:apply-templates>

 </xsl:element>

</xsl:template>

 <xsl:templatematch="book">

    <xsl:elementname="Author">

      <xsl:value-ofselect="author/first-name"/>

      <xsl:text> </xsl:text>

      <xsl:value-ofselect="author/last-name"/>

    </xsl:element>

   

 </xsl:template>

</xsl:stylesheet>

8、 For XML AUTO




9\附件:

Book.xml源文件内容:

<?xmlversion="1.0"encoding="utf-8" ?>

<!--这是注释-->

<bookstorexmlns="http://www.inuli.cn">

 <bookgenre="autobiography"publicationdate="1981"ISBN="1-861003-11-0">

    <title>The Autobiography of Benjamin Franklin</title>

    <author>

      <first-name>Benjamin</first-name>

      <last-name>Franklin</last-name>    

    </author>

    <price>8.99</price>

 </book>

 <bookgenre="novel"publicationdate="1967"ISBN="11-22-2323-0">

    <title>The Confidence Man</title>

    <author>

      <first-name>Herman</first-name>

      <last-name>Melville</last-name>

    </author>

    <price>11.99</price>

 </book>

 <bookgenre="philosoph"publicationdate="1991"ISBN="1-644343-33-3">

    <title>The Gorgias</title>

    <author>

      <first-name>Sidas</first-name>

      <last-name>Plato</last-name>

    </author>

    <price>9.99</price>

 </book>

</bookstore>

posted @ 2008-03-11 09:37  一滴海水  阅读(303)  评论(1编辑  收藏  举报