安卓在xml设置文字字体

XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a

XML文字是处理XML文件的一种很好的方法,社区并未充分使用它。 XML文字就像


"), it begins with (<) and ends with (>).  You can use regular XML tags right there in the code!
“ )开头和结尾,而是以( < )和( >

介绍
(Introduction
)

XML文字允许您在代码中使用XML语法。 因为您在代码中有了标签,所以以这种方式使用XML文件和XML片段很容易,但是与使用

XmlDocument and

XmlDocument

XmlElement.

XmlElement的传统方法相比,它也可以更快地访问信息。


It’s available in the namespace System.Xml.Linq, since the .NET Framework 3.5/Visual Studio 2008 (for Visual Basic Only) it supports most of the Extensible Markup Language (XML) 1.0 specification, and together with Lambda Expressions and/or LINQ, gives you a better experience with XML files.  It’s also recognized by the intellisense system, making it very convenient to use.  It even does the indenting automatically.

自.NET Framework 3.5 / Visual Studio 2008(仅用于Visual Basic)以来,它在名称空间System.Xml.Linq中可用,它支持大多数可扩展标记语言(XML)1.0规范 ,以及Lambda表达式和/或LINQ。 ,可为您提供更好的XML文件体验。 它也被智能感知系统所认可,使用起来非常方便。 它甚至自动执行缩进。

(Topics in this article:)



XML Literals

XML文字






Read Information

阅读信息






List Information

清单信息






Embedded Expressions

嵌入式表达式






Modify Nodes

修改节点






Inserting Nodes

插入节点






Deleting Nodes

删除节点






Finally Example (web)

最终示例(网络)






XML Literals

XML文字


NOTE: Most of the examples use


Option Infer On but you can declare the correct variables data type.

Option Infer On,

The basic concept looks like this:

基本概念如下所示:

Dim msg = <msg> 
                      This is a test! 
                      This is a test! 
                  </msg>.Value 
        MessageBox.Show(msg, "XML Literals")

_” (which, incidentally, will not even be necessary in Visual Studio 2010 for most of the code).

_



You can create your XML file in runtime mode.  Here’s an example how to achieve that:

您可以在运行时模式下创建XML文件。 这是一个实现该目标的示例:

Dim bookList = _ 
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <!-- List of books and magazines --> 
    <library> 
        <books> 
            <book name="The Hunger Games" author="Suzanne Collins"/> 
            <book name="Breaking Dawn" author="Stephenie Meyer"/> 
            <book name="The Last Song" author="Nicholas Sparks"/> 
        </books> 
        <magazine> 
            <magazineName>"MSDN Magazine"</magazineName> 
            <magazineName>"Code Magazine"</magazineName> 
        </magazine> 
    </library>

bookList is now an XDocument that you can work as an XML file. To save the file on the disk, you just need to use the Save() method:

变量XDocument ,您可以将其用作XML文件。 要将文件保存在磁盘上,只需使用Save()方法:

bookList.Save("c:\library.xml")

Read Information

阅读信息

The previous example generates an easy XML file and saves it to disk. To load the file and handle it, you can use the Load() method. This will load the file and show the magazine name, using the Descendants property.

前面的示例生成一个简单的XML文件并将其保存到磁盘。 要加载并处理文件,可以使用Load()方法。 这将使用Descendants属性加载文件并显示杂志名称。


triple-dot (...), from a XElement or XDocument object:

属性允许您使用名称从XElementXDocument对象使用三点(...)访问后代节点:

Dim xmlFile = XDocument.Load("c:\library.xml") 
Debug.WriteLine(xmlFile...<magazineName>.Value)
Dim xmlFile = XDocument.Load("c:\library.xml") 
Debug.WriteLine(xmlFile...<magazineName>.Where(Function(f) _ 
                               f.Value = "Code Magazine").Value)
Dim xmlFile = XDocument.Load("c:\library.xml") 
Debug.WriteLine(xmlFile...<book>.@author)
Dim xmlFile = XDocument.Load("c:\library.xml") 
Debug.WriteLine(xmlFile...<book>.Where(Function(f) _ 
                                 f.@name = "Breaking Dawn").@author)

List Information

清单信息

You have several ways to show information, looping on the values, using LINQ to XML or Lambda Expressions. You can list all the magazines this way:

使用LINQ to XML或Lambda表达式,您可以通过多种方法来显示信息,在值上循环。 您可以通过以下方式列出所有杂志:

For Each m In From element In bookList.<library>.<magazine>.<magazineName> 
   Debug.WriteLine(m.Value)

Descendants

Descendants属性(...)并显着简化代码:

For Each m In From element In bookList...<magazineName> 
    Debug.WriteLine(m.Value) 
Next

To show the book names you can use the same method, but since you now deal with attributes, you use the “@” to define that you want the attribute, plus the attribute name.

要显示书名,您可以使用相同的方法,但是由于现在要处理属性,因此使用“ @”来定义所需的属性以及属性名称。

For Each book In From element In bookList...<book> 
    Debug.WriteLine("Book: " & book.@name.ToString) 
    Debug.WriteLine("Author: " & book.@author.ToString) 
    ' Separation line 
    Debug.WriteLine(New String("-"c, 40)) 
Next

But you can also filter the information before showing it.  This example uses LINQ to XML to check all of the books that have a name containing the keyword “Song”.

但是您也可以在显示信息之前过滤信息。 本示例使用LINQ to XML来检查所有名称包含关键字“ Song”的书籍。

' Using LINQ to XML to filter the information 
Dim bookSearch = From b In bookList...<book> _ 
         Where b.@name.ToString.Contains("Song") _ 
         Select b.@name, b.@author 

' Show the results 
For Each book In From element In bookSearch 
    Debug.WriteLine("Book: " & book.name) 
    Debug.WriteLine("Author: " & book.author)     

    ' Separation line 
    Debug.WriteLine(New String("-"c, 40)) 
Next

Embedded Expressions

嵌入式表达式

Embedded expressions are expressions that you can use in the XML code, using the tags

嵌入式表达式是可以在XML代码中使用标签的表达式

<%= expression %>

<%=表达式%>

much the same wasy as it's done in ASP.NET.  You can use them to build or modify the XML file and that makes it really easy to create a file from a DataTable, List(Of T), Dictionary, etc.

与在ASP.NET中所做的工作几乎相同。 您可以使用它们来构建或修改XML文件,这使得从DataTable,List(Of T),Dictionary等创建文件确实非常容易。

Here’s a very straightforward example, using a Func() delegate (Lambda Expression) that adds two values:

这是一个非常简单的示例,使用Func ()委托(Lambda Expression)添加两个值:

Dim f As Func(Of Integer, Integer, Integer) = Function(x, y) x + y 

Dim example = _ 
    <test> 
        <value><%= f(125, 125).ToString() %></value > 
    </test>

The result will be:

结果将是:

 

<value>250</value> 
 <value> 250 </ value> 
 </test> 
 </ test>

' Creates a list with some book names  
Dim bookList As New List(Of String) 
bookList.AddRange(New String() {"The Hunger Games", "Breaking Dawn", "The Last Song"}) 

' Creates the XML e saves it to disk 
Dim newBookList1 = _ 
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
    <library> 
        <books> 
            <%= From b In bookList Select <book><%= b %></book> %> 
        </books> 
    </library> 

newBookList1.Save("c:\result.xml")

 

<library> 
 <图书馆> 
          <books> 
 <书> 
                <book>The Hunger Games</book> 
 <book>饥饿游戏</ book> 
                <book>Breaking Dawn</book> 
 <book>破晓</ book> 
                <book>The Last Song</book> 
 <book>最后一首歌</ book> 
          </books> 
 </ books> 
     </library>
 </ library>

' For this example is created a DataTable manually but 
' could be the result of a SQL query or stored procedure 
Dim dt As New DataTable("Books") 
dt.Columns.Add("Book", GetType(String)) 
dt.Columns.Add("Author", GetType(String)) 
dt.Rows.Add("The Hunger Games", "Suzanne Collins") 
dt.Rows.Add("Breaking Dawn", "Stephenie Meyer") 
dt.Rows.Add("The Last Song", "Nicholas Sparks") 
Dim ds As New DataSet 
ds.Tables.Add(dt) 

' Creates the XML e with two attributes: "name" and "author"  
Dim newBookList2 = _ 
   <?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
   <!-- my book list --> 
   <library> 
       <books> 
           <%= From b In ds.Tables("Books") Select _ 
               <book name=<%= b.Item("Book") %> 
                   author=<%= b.Item("Author") %>/> %> 
       </books> 
   </library> 

' Saves it to disk 
newBookList2.Save("c:\library.xml")

 

<library> 
 <图书馆> 
             <books> 
 <书> 
                 <book name=" The Hunger Games" author="Suzanne Collins" /> 
 <book name =“ The Hunger Games” author =“ Suzanne Collins” /> 
                 <book name=" Breaking Dawn" author="Stephanie Meyer" /> 
 <book name =“ Breaking Dawn” author =“ Stephanie Meyer” /> 
                 <book name=" The Last Song" author="Nicholas Sparks" /> 
 <book name =“ The Last Song” author =“ Nicholas Sparks” /> 
             </books> 
 </ books> 
     </library>
 </ library>

Modify Nodes

修改节点

To modify any information in a XML file, using XML Literals, you just need to read the file, change the value and then save it to disk.

要使用XML Literals修改XML文件中的任何信息,您只需要读取文件,更改值然后将其保存到磁盘即可。

Here’s an example:

这是一个例子:

Dim xmlFile = XDocument.Load("c:\library.xml") 
xmlFile...<magazineName>.Value = "New Value" 
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml") 
Dim element = xmlFile.<library>.<books>.<book>.Where(Function(f) _ 
                             f.@name = "The Last Song") 
element.@author = "Jorge Paulino" 
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml") 
xmlFile...<book>.Where(Function(f) _ 
                f.@name = "The Last Song").@author = "Jorge Paulino" 
xmlFile.Save("c:\library.xml")

Inserting Nodes

插入节点

To insert a new node into the XML file, you first need to build the new element (XElement) and then add it to the right position. We can do that in two ways:

要将新节点插入XML文件,首先需要构建新元素(XElement),然后将其添加到正确的位置。 我们可以通过两种方式做到这一点:

Dim xmlFile = XDocument.Load("c:\library.xml") 
Dim element = New XElement("book", _ 
                    New XAttribute("name", "XML Literals"), _ 
                    New XAttribute("author", "Jorge Paulino")) 
Dim parent = xmlFile...<books>.FirstOrDefault() 
parent.Add(element) 
xmlFile.Save("c:\library.xml")
Dim xmlFile = XDocument.Load("c:\library.xml") 
Dim element = <book name="XML Literals" author="Jorge Paulino"/> 
Dim parent = xmlFile...<books>.FirstOrDefault() 
parent.Add(element) 
xmlFile.Save("c:\library.xml")

Deleting Nodes

删除节点

Deleting a node is very similar to the modification method.  You can remove all the nodes:

删除节点与修改方法非常相似。 您可以删除所有节点:

Dim xmlFile = XDocument.Load("c:\library.xml") 
xmlFile...<magazineName>.Remove() 
xmlFile.Save("c:\library.xml")

Or remove a specific node:

或删除特定节点:

Dim xmlFile = XDocument.Load("c:\library.xml") 
xmlFile...<book>.Where(Function(f) f.@author = "Suzanne Collins").Remove() 
xmlFile.Save("c:\library.xml")

Finally Example (web)

最终示例(网络)

You can use XML Literals also to read information from the web, like RSS.  This example reads my personal blog RSS and filters by the “VB.NET” category (that is defined by the tags).  This shows how powerful and easy is to work with XML Literals.

您还可以使用XML Literals从Web读取信息,例如RSS。 本示例读取我的个人博客RSS,并按“ VB.NET”类别(由标记定义)进行过滤。 这显示了使用XML Literals多么强大和容易。

Dim xmlFile = XDocument.Load("http://feeds.feedburner.com/vbtuga") 
Dim blogList = xmlFile...<item>.Where(Function(f) _ 
                f.<category>.Value = "VB.NET").<title>.ToList() 
For Each item As XElement In blogList 
    Console.WriteLine(item.Value) 
Next 
Console.ReadKey()

And here's the resulting console output:

这是结果控制台输出:


xmlliterals2.jpg

xmlliterals2.jpg


Conclusion

结论

XML literals provide several methods to work with XML files. Today you have XML files for everything (reports, configurations, data storage, RSS, etc) and it’s so important to handle it right, quickly and without complications.

XML文字提供了几种处理XML文件的方法。 今天,您已经拥有所有内容的XML文件(报告,配置,数据存储,RSS等),因此正确,快速且无复杂性地处理它至关重要。

This is an English version of some articles I posted a while ago in my blog and I hope this article helps you to become better with XML Literals, and use them more!