| 网站首页 | VB.Net实例分析 | .Net技术文摘 | 下载中心 | VB.Net书籍笔记 | .Net源码 | VBS脚本与组件调用 | Windows2008技术文摘 | 给我们留言 | 
最新公告:

  没有公告

您现在的位置: 乐博网 >> VB.Net实例分析 >> 数据库编程 >> 实例分析正文
最新推荐 更多内容
LINQ to XML使用入门
LINQ to XML使用入门
作者:Akyao    来源:乐博网整理     更新时间:2011-6-27

本文演示LINQ to XML使用入门,来自乐博网lob.cn。

如果你想下载本文的源代码RAR压缩集合包  请访问
VB2010源代码集合包(芋头糕)    http://www.lob.cn/code/utility/2795.shtml
特别感谢网友 芋头糕 将此资源提供乐博网分享,欢迎加入 40797788 的.Net超级qq群交流。

LINQ to XML使用入门 的代码 如下:

Imports <xmlns='urn:art-org:art'>
Imports <xmlns:a='urn:art-org:artists'>
Imports System.Xml

Module Module1

    Sub Main()
        ' construct the predefined document
        ' using the XML Literals approach
        Dim document = CreateDocumentLiteral()
        Console.WriteLine(document)

        ' dump all nodes to the console
        DumpNode(document)

        ' construct the predefined document again
        ' using the XML Literals approach with imported XML Namespaces
        document = CreateDocumentLiteralWithImportedNamespaces()
        Console.WriteLine(document)

        ' dump all nodes to the console
        DumpNode(document)

        ' construct the predefined document again
        ' using the XML DOM-like approach
        document = CreateDocumentVerbose()

        ' display the document in the console
        Console.WriteLine(document)

        ' dump all nodes to the console
        DumpNode(document)

        ' construct the predefined document again
        ' with the functional approach
        document = CreateDocumentConcise()

        ' display the document in the console
        Console.WriteLine(document)

        ' dump all nodes to the console
        DumpNode(document)

        ' find all elements named 'artist' using Elements property
        FindChildren(document)

        ' find all elements named 'artist' using Descendants property
        FindDescendants(document)

        ' find attribute named 'name' using Attribute property
        FindAttribute(document)

        Console.ReadLine()
    End Sub
    ' <?xml version="1.0"?>
    ' <?order alpha ascending?>
    ' <art xmlns='urn:art-org:art'>
    '   <period name='Renaissance' xmlns:a='urn:art-org:artists'>
    '     <a:artist>Leonardo da Vinci</a:artist>
    '     <a:artist>Michelangelo</a:artist>
    '     <a:artist><![CDATA[Donatello]]></a:artist>
    '   </period>
    '   <!-- insert period here -->
    ' </art>
    Function CreateDocumentLiteral() As XDocument
        Return <?xml version="1.0"?>
               <?order alpha ascending?>
               <art xmlns='urn:art-org:art'>
                   <period name='Renaissance' xmlns:a='urn:art-org:artists'>
                       <a:artist>Leonardo da Vinci</a:artist>
                       <a:artist>Michelangelo</a:artist>
                       <a:artist><![CDATA[Donatello]]></a:artist>
                   </period>
                   <!-- insert period here -->
               </art>
    End Function

    ' <?xml version="1.0"?>
    ' <?order alpha ascending?>
    ' <art xmlns='urn:art-org:art'>
    '   <period name='Renaissance' xmlns:a='urn:art-org:artists'>
    '     <a:artist>Leonardo da Vinci</a:artist>
    '     <a:artist>Michelangelo</a:artist>
    '     <a:artist><![CDATA[Donatello]]></a:artist>
    '   </period>
    '   <!-- insert period here -->
    ' </art>
    Function CreateDocumentLiteralWithImportedNamespaces() As XDocument
        Return <?xml version="1.0"?>
               <?order alpha ascending?>
               <art>
                   <period name='Renaissance'>
                       <a:artist>Leonardo da Vinci</a:artist>
                       <a:artist>Michelangelo</a:artist>
                       <a:artist><![CDATA[Donatello]]></a:artist>
                   </period>
                   <!-- insert period here -->
               </art>
    End Function

    Function CreateDocumentVerbose() As XDocument
        Dim nsArt As XNamespace = "urn:art-org:art"
        Dim nsArtists As XNamespace = "urn:art-org:artists"

        ' create the document
        Dim document = New XDocument()

        ' create the xml declaration and
        ' set on the document
        document.Declaration = New XDeclaration("1.0", Nothing, Nothing)

        ' create the art element and
        ' add to the document
        Dim art = New XElement(nsArt + "art")
        document.Add(art)

        ' create the order processing instruction and
        ' add before the art element
        Dim pi = New XProcessingInstruction("order", "alpha ascending")
        art.AddBeforeSelf(pi)

        ' create the period element and
        ' add to the art element
        Dim period = New XElement(nsArt + "period")
        art.Add(period)

        ' add the name attribute to the period element
        period.SetAttributeValue("name", "Renaissance")

        ' create the namespace declaration xmlns:a and
        ' add to the period element
        Dim nsdecl = New XAttribute(XNamespace.Xmlns + "a", nsArtists)
        period.Add(nsdecl)

        ' create the artists elements and
        ' the underlying text nodes
        period.SetElementValue(nsArtists + "artist", "Michelangelo")

        Dim artist = New XElement(nsArtists + "artist", "Leonardo ", "da ", "Vinci")
        period.AddFirst(artist)

        artist = New XElement(nsArtists + "artist")
        period.Add(artist)
        Dim cdata = New XText("Donatello")
        artist.Add(cdata)

        ' create the comment and
        ' add to the art element
        Dim comment = New XComment("insert period here")
        art.Add(comment)

        Return document
    End Function
    Function CreateDocumentConcise() As XDocument
        Dim nsArt As XNamespace = "urn:art-org:art"
        Dim nsArtists As XNamespace = "urn:art-org:artists"

        ' create the document all at once
        Return New XDocument( _
                    New XDeclaration("1.0", Nothing, Nothing), _
                    New XProcessingInstruction("order", "alpha ascending"), _
                    New XElement(nsArt + "art", _
                        New XElement(nsArt + "period", _
                            New XAttribute("name", "Renaissance"), _
                            New XAttribute(XNamespace.Xmlns + "a", nsArtists), _
                            New XElement(nsArtists + "artist", "Leonardo da Vinci"), _
                            New XElement(nsArtists + "artist", "Michelangelo"), _
                            New XElement(nsArtists + "artist", _
                                New XText("Donatello"))), _
                        New XComment("insert period here")))
    End Function

    Sub DumpNode(ByVal node As XNode)
        Select Case node.NodeType
            Case XmlNodeType.Document
                Dim document = CType(node, XDocument)
                Console.WriteLine("StartDocument")
                Dim declaration = document.Declaration
                If declaration IsNot Nothing Then
                    Console.WriteLine("XmlDeclaration: {0} {1} {2}", declaration.Version, declaration.Encoding, declaration.Standalone)
                End If
                For Each n In document.Nodes()
                    DumpNode(n)
                Next
                Console.WriteLine("EndDocument")
            Case XmlNodeType.Element
                Dim element = CType(node, XElement)
                Console.WriteLine("StartElement: {0}", element.Name)
                If element.HasAttributes Then
                    For Each attribute In element.Attributes()
                        Console.WriteLine("Attribute: {0} = {1}", attribute.Name, attribute.Value)
                    Next
                End If
                If Not element.IsEmpty Then
                    For Each n In element.Nodes()
                        DumpNode(n)
                    Next
                    End If
                Console.WriteLine("EndElement: {0}", element.Name)
            Case XmlNodeType.Text
                Dim text = CType(node, XText)
                Console.WriteLine("Text: {0}", text.Value)
            Case XmlNodeType.ProcessingInstruction
                Dim pi = CType(node, XProcessingInstruction)
                Console.WriteLine("ProcessingInstruction: {0} {1}", pi.Target, pi.Data)
            Case XmlNodeType.Comment
                Dim comment = CType(node, XComment)
                Console.WriteLine("Comment: {0}", comment.Value)
            Case XmlNodeType.DocumentType
                Dim documentType = CType(node, XDocumentType)
                Console.WriteLine("DocumentType: {0} {1} {2} {3}", documentType.Name, documentType.PublicId, documentType.SystemId, documentType.InternalSubset)
        End Select
    End Sub

    Sub FindChildren(ByVal document As XDocument)
        ' use Elements property on an XDocument object to retrive all elements named 'artist'
        Dim results = document.<art>.<period>.<a:artist>
        For Each element In results
            Console.WriteLine("Child 'artist' Element: " & element.Value)
        Next

        ' use Elements property on an XElement object to retrive all elements named 'artist'
        Dim periodElement = document.<art>.<period>(0)
        results = periodElement.<a:artist>
        For Each element In results
            Console.WriteLine("Child 'artist' Element: " & element.Value)
        Next
    End Sub

    Sub FindDescendants(ByVal document As XDocument)
        ' use Descendants property on an XDocument object to retrive all elements named 'artist'
        Dim results = document...<a:artist>
        For Each element In results
            Console.WriteLine("Descendant 'artist' Element: " & element.Value)
        Next

        ' use Descendants property on an XElement object to retrive all elements named 'artist'
        Dim artElement = document.<art>(0)
        results = artElement...<a:artist>
        For Each element In results
            Console.WriteLine("Descendant 'artist' Element: " & element.Value)
        Next
    End Sub

    Sub FindAttribute(ByVal document As XDocument)
        ' use Attribute property as well as Elements property to retrive first attribute named 'name'
        ' inside element named 'period' within an XDocument object
        Dim result = document.<art>.<period>.@name
        Console.WriteLine("'name' Attribute: " & result)

        ' use Attribute property as well as Descendants property to retrive first attribute named 'name'
        ' inside element named 'period' within an XDocument object
        result = document...<period>.@name
        Console.WriteLine("'name' Attribute: " & result)

        ' use Attribute property as well as Descendants property to retrive first attribute named 'name'
        ' inside element named 'period' within an XElement object
        Dim periodElement = document...<period>(0)
        result = periodElement.@name
        Console.WriteLine("'name' Attribute: " & result)
    End Sub
End Module

  • 上一篇:

  • 下一篇: 没有了
  • 【字体: 】【打印此文】【关闭窗口
      相关文章:(只显示最新16条)
    将LINQ to XML代码绑定到WPF控件
    使用LINQ To SQL查询数据库的基本示例
    LINQ查看表达式树的方法(VB2010实例)
    在运行时创建LINQ查询的代码(VB2010实例)
    VB.Net使用LINQ获取单个进程使用的最大物理内存量
    VB.Net在LINQ中使用正则表达式设置查询条件
    VB.Net在LINQ to XML中通过内存将属性转换为元素
    VB.Net在LINQ to XML中使用XPath查询指定元素值
    VB.Net股票行情在LINQ to XML中执行文本到XML的流式转…
    VB.Net在LINQ to XML中将CSV文件转换为XML文件
    VB.Net在LINQ to XML中将XML文件转换为CSV文件
    VB.Net根据LINQ to DataSet查询创建DataView
    VB.Net返回LINQ to DataSet中指定行后的剩余记录
    VB.Net在LINQ to DataSet中根据字符串的长度排序
    VB.Net使用ToArray()方法强制立即执行LINQ查询
    VB.Net在LINQ to DataSet中使用联接实现交叉表查询

    | 设为首页 | 加入收藏 | 联系站长 | | 友情链接 | 版权申明 |
    乐博网欢迎各种媒体转载我们的原创作品[转载请注明出处];我们鼓励更多VB.Net开发者一起加入研究与探讨;如发现文章访问错误、内容错误或版权疑问、内容有违相关法律(如涉及政治、色情、反动或散布虚假有害信息)等情况,请及时向我们举报,我们将及时纠正!
    联系邮箱:Shiny#vip.qq.com (#替换为@) QQ交流群: 40797788 [闽ICP备05014267号]