1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual studio 2010 part 40 pptx

6 96 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Introduction to XML
Trường học University of Example
Chuyên ngành Computer Science
Thể loại Bài viết
Năm xuất bản 2010
Thành phố Example City
Định dạng
Số trang 6
Dung lượng 112,88 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Listing A-1 shows an XML document that holds customer data.. A value can also be one or more elements, such as customer, in Listing A-1, which contains name and address elements.. Rememb

Trang 1

Introduction to XML

Trang 2

Extensible Markup Language (XML) is an open-standards cross-platform way of

specifying documents At its origins, XML was used to represent data, but it has grown

in use to include user interface technologies and even executable logic While there are many practical uses of XML, this book is mostly concerned with explaining how XML is used for ASP.NET, Silverlight, and Windows Presentation Foundation (WPF), all of which are discussed in chapters of this book In each of these scenarios, some specialization of XML is being used to construct user interfaces In ASP.NET, you use XML for HTML (XHTML) Both Silverlight and WPF use XML Application Markup Language (XAML), pronounced “Zamel.” Before learning about XHTML or XAML, you might want an introduction or refresher on XML, which is the purpose of this appendix While this introduction won’t teach you everything about XML, it will give you the essentials that can help when seeing how XML is being used

VS 2010 XML Editor

You can create your own XML documents in VS 2010 with the XML editor There are a couple of ways to open a new XML document, within or without a project Without a project, select File | New | File and select XML File, and click OK You can rename the file (for instance, Customer.xml) when saving Within a project, right-click the project, select Add | New Item, select the Data list, select XML File, give the document a name (for instance, Customer.xml), and click OK What this gives you is an editor with Intellisense support that is better than Notepad Listing A-1 shows an XML document that holds customer data

Listing A-1 An XML document example

<?xml version="1.0" encoding="utf-8" ?>

<customer id="7">

<name>Joe</name>

<address>123 4th St</address>

</customer>

As you can see in Listing A-1, an XML document is readable text It contains data, and the meaning of that data is specific to the applications that need to use it The following sections will decipher Listing A-1 and explain what each part of the document means

XML Prefixes

The top of the document in Listing A-1 contains an XML prefix, repeated here for convenience:

<?xml version="1.0" encoding="utf-8" ?>

Trang 3

The prefix is common for letting applications reading the document know that it is

indeed an XML document The version is self-describing Encoding is important because

it specifies the binary format of the text If you have one application passing data to

another application, it’s important that both applications can read the document and are

using the same encoding The utf-8 encoding is the default and for the purpose of this

book is the only encoding you will care about

The angle brackets, < and >, define the markup in XML For the file prefix, content is placed between <? and ?> character sequences, but as the following sections show, most

other markup is different

XML Elements

The XML elements in Listing A-1 are customer, name, and address Each element is

defined by matching pairs of markup, following this pattern:

<elementName>value</elementName>

In the previous example, elementName is the name of the element and value is the data

associated with that element Elements always have a begin tag and an end tag You can

identify the end tag because it always follows the begin tag eventually (there may be other element tags nested in between the pair) and contains a forward slash character before the

element name

The value in the previous example can sometimes be blank, meaning there is no value

for that element A value can also be one or more elements, such as customer, in Listing A-1,

which contains name and address elements In Listing A-1, the value of name is Joe and the

value of address is 123 4th St In addition to elements, you can have attributes, discussed next.

Attributes

An attribute decorates an element with a single value, such as in the following example:

<elementName attributeName="attributeValue">

elementValue

</elementName>

Notice that the attribute, attributeName, is inside of the start tag of the element It

contains an equal sign and a quoted value You can have multiple attributes on a single

element and they’ll be separated by spaces Remember that attributes can have only one

value, but if you need to define more than one value, you must use elements

Examples of attributes in Listing A-1 are version and encoding in the prefix and id

on customer

Trang 4

Another important part of XML that you’ll need to understand is namespaces In Chapter 2, you learned how namespaces in C# and VB help give a unique identity to code within a given namespace The purpose of namespaces in XML is similar In the case of Listing A-1, there is a customer element, but think about how many different programs work with customer data A customer in one program will not be defined the same as a customer in another program, and you need a way to tell them apart, which is where namespaces come

in You would define your customer data in a namespace of your choosing, and some other developer would define a unique namespace for their customer That way, your programs won’t ever be confused if they try to read the wrong data Listing A-2 shows how to use a namespace to make a customer unique

TIP

You might have noticed that the namespaces in Listing A-2 look like Web addresses

However, this is just coincidence and is a common practice used to increase the chance

that the namespace is unique In reality, the namespace is just a string, which catches

people new to namespaces off guard For example, http://mcgraw-hill.com/vs2010bg

is a different namespace than http://mcgraw-hill.com/vs2010bg/ because the extra

forward slash on the end is a different string So, if you made this mistake, then it’s

possible that a program won’t recognize the data as being a valid format because the

data is in a different namespace than what the program expects Remember that a

namespace is a unique string, not a Web address.

Listing A-2 XML namespace example

<?xml version="1.0" encoding="utf-8" ?>

<customer id="7"

xmlns="http://mcgraw-hill.com/vs2010bg"

xmlns:a="http://somedomain.com/addresses">

<name>Joe</name>

<a:address>123 4th St</a:address>

</customer>

Namespaces are specified by placing an xmlns attribute on an element, either with

or without a prefix The xmlns without a prefix specifies the default namespace for all of

the elements where the namespace resides and child elements of the element where the

namespace resides This means that customer and name are in the http://mcgraw-hill.com/

vs2010bg namespace

Namespaces can also have prefixes to help you target where they are applied In

Listing A-2, there is an xmlns:a, where a is the prefix for the http://somedomain.com/

Trang 5

addresses namespace The convenience of prefixes is that they help the XML be more

readable In Listing A-2, the address namespace is decorated with the a: prefix, as in

<a:address> to indicate that address belongs to the http://somedomain.com/addresses

namespace Without the prefix, you would be forced to write the address element as

follows, which is more difficult to read:

< http://somedomain.com/addresses:address>

123 4th St

</ http://somedomain.com/addresses:address>

I added line breaks for readability, but in practice the only part of the data read is the

value and not the white space, such as newlines, surrounding it

The XML Menu

When you open an XML document in VS, you’ll see an XML menu appear with options

for running, debugging, and profiling XML Transformation (XSLT) documents and

working with schemas XSLT is used by a running program or utility to change an

XML document from one form to another An XML schema is an XML document that

describes the allowable format of another XML document An XML schema is to an XML document what a SQL table definition is to the data that the table holds Both XSLT and

schemas are outside the scope of this book, but now you know where the tools are in case

you need to work with them

Configuring XML Options

Selecting Tools | Options will open the VS Options window From the Options window,

you can select Text Editor XML and configure many options associated with writing XML documents, such as turning on line numbering or specifying tag formatting

Summary

You should now understand the basics of working with XML in VS You learned how to

create an XML document and what prefixes, elements, attributes, and namespaces are

You also learned how to find the XML options to customize your XML document-editing

experience XML is the foundation upon which XAML and XHTML are based, which

is covered in later appendices This should give you familiarity with the XML that is

presented in the chapters of this book

Ngày đăng: 04/07/2014, 03:20