C # 30 ADO and XML

Source: Internet
Author: User

We often say the data set as a temporary database, if we compare the data set this temporary database to a warehouse, then XML is a train, ship container, the data set is responsible for temporary storage of data, XML is responsible for shipping data as the container, XML is even more important than the dataset. XML full Extensiblemarkup Language, translated into Extensible Markup Language, is mainly used as a carrier for transmitting data between systems and systems.

Focus:

? Syntax FOR XML

? The composition of XML

? How to implement a variety of data and XML transformations

Preview Lessons:

? Introduction and origin of XML

? The composition of XML

? Syntax FOR XML

? transformation of XML and data tables

Introduction to 7.1 XML

XML represents extensible Markup Language (extensible markuplanguage abbreviation, meaning Extensible Markup Language). XML is a set of rules that define semantic markup that divides a document into parts and identifies the parts. It is also a meta-markup language that defines syntactic languages used to define other, semantic, structured markup languages that are relevant to a particular domain.

7.1.1 What are the main differences between XML and HTML?

XML is used to store data.

XML is not an alternative to HTML, and XML and HTML are two languages that are used for different purposes.

XML is designed to describe data, focusing on what the data is and how it is stored.
HTML is designed to display data, with the emphasis on displaying data and how to display it better.

HTML is associated with displaying information, and XML is related to descriptive information.

7.1.2 Use of XML


Üxml can detach data from HTML

Üxml for exchanging data and sharing data

Üxml can be used to store data

Joke: Write XML on your resume.

7.2 Syntax FOR XML

for a beginner of XML, first you need to understand the syntax of XML. Once you understand the syntax of XML, you can define your own XML tags, and you can create XML documents to store your data with your own defined tags.

7.2.1 part of an XML document

ü The various components of an XML document for expressing data in a hierarchical order are:

n processing instruction (PI)

n identifiers

n Element

N Content

N Property

N Entities

N Comments

The following cases:

<?xmlversion= "1.0" encoding= "GBK"?> <!--processing instructions (PI)

Provides information about how the XML file should be handled. -

< warehouse data ><!-root elements--

<!--warehouse data is the root element--<!--child element--

< Stock Number = "S101" > <!-number is attribute--

< product name >toys</Product name >
< quantity >100</Quantity >

< Show information > toy prices &lt; </Display Info >

</stock >

</Warehouse Data >

Toys 100 and so on are the contents of XML

Content refers to the information represented by the elements of an XML document. An element can contain:

? Character or data content

? Element content

? Combine or mix content

For an XML document, each label can be thought of as a constituent element of an XML document, a basic unit of an XML document, with the following basic format:

< Tag Name property = attribute value > element value </tag name >

The rules for naming tags in XML must be followed:

The name of the L element can contain letters, numbers, and other characters

The name of the L element cannot begin with a number or punctuation mark

The name of the L element cannot be in XML (or XML, XML, XML ...). ) Start

The name of the L element cannot contain spaces

7.2.2 XML Syntax considerations

The declaration statement in the XML document should be placed in the first sentence:

such as: <?xmlversion= "1.0" encoding= "GB2312"?>

The XML tag definition is case-sensitive, as in the error format

<BookName> Fog Orphans </bookName>

l have an end tag for each label, but if the label has no content it can combine two tags. such as: <user id= "Kobe"/> equivalent to <user id= "Kobe" ></User>

The value of the L attribute is quoted, which is different from HTML, such as:

<font color= "Red" >J2EE course </font>

L An XML document has only one root node, which can be duplicated in addition to the root node.

When writing XML, be aware of the indentation of the line of code, which is actually a tree-shaped structure that the XML document describes.

Project:

Convert the following primary, from table to an XML file



7.3.1 RSS Format Really Simple Syndication

<rss version= "2.0" >

<channel>

<title> Sina News </title>

<description> Sina News Channel </description>

<link>http://rss.sina.com.cn/news/marquee/ddt.xml</link>

...

<item>

<title> Owen Milestone goal straight chase three legends </title>

<link>http://sports.sina.com.cn ...

06383166011.shtml</link>

<description> European Championship Qualifiers </description>

</item>

...

</channel>

</rss>

The XML is described in detail as follows:

<rss version= "2.0" >

<channel> <!--Channel nodes-->

<title></title><!--Channel Header node-->

<description></description><!--Channel Description-->

<link></link> <!--Channel link URL-->

...

<item> <!--article project node-->

<title></title> <!--article title-->

<link></link> <!--articles links url-->

<description></description><!--Article Description-->

</item>

...

</channel>

</rss>

7.3.2 reading data from an XML file

The Student.xml file is as follows:

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

<Student>

<Name> Zhang </Name>

<Age>20</Age>

<Hobby> Sing Song </Hobby>

<!---->

<Name> Zhoujeje </Name>

<Age>22</Age>

<Hobby> double-knuckle sticks </Hobby>

</Student>

The source code for reading data from an XML file using a C # program is as follows:

Usingsystem;

UsingSystem.Collections.Generic;

Usingsystem.text;

Usingsystem.xml;

Namespace ReadXml

{

Class Program

{

static void Main (String[]args)

{

XmlDocument myXML = Newxmldocument ();

Myxml.load ("Student.xml"); Reads the specified XML document

XmlNode student =myxml.documentelement;//Read the root node of XML

foreach (XmlNode node instudent. ChildNodes)//Loop the child nodes

{

Display the contents of each node

Switch (node. Name)

{

Case "Name":

Console.WriteLine ("Name: {0}", node.) InnerText);

Break

Case ' age ':

Console.WriteLine ("Age: {0}", node.) InnerText);

Break

Case "Hobby":

Console.WriteLine ("Hobby: {0}", node.) InnerText);

Break

}

}

}

}

}

? The XmlDocument object represents the entire XML document

? The XmlNode object represents a single node of the XML file

?


7.3.3 Crawl article List steps



Traversing XML uses the following steps:

? Add Fetcharticles () in the RssFeed class

? Using a double cycle



Code See Mynewsreader Solution

To read an XML file into the TreeView control



The node of the TreeView is similar to the node for XML

foreach (set loop channel node)

{//treeview.nodes and Xmldocument.childenodes comparison

foreach (set loop article node)

{//Treenode.nodes and Xmlnode.childenodes

Set the value of the article

}

}

The TreeView also uses nodes to represent data

Pm. Load ();//Get channel information

foreach (RssFeed feed inpm. profile.feeds)//cycle by Channel

{

TreeNode Feednode;

Feednode =tvmain.nodes.add (feed. DisplayName);

foreach (article Atcl infeed. Articles.values)

{//By article List loop

FEEDNODE.NODES.ADD (ATCL. Title);

}

}

Right-click to update selected Channel News

TreeNode node =tvmain.selectednode;

...

if (PM. Profile.feeds[node. Index]. Fetcharticles ())

{

Call to update specified channel method

Updatearticlesview (node. Index);

}

Tvmain.beginupdate (); Suppress tree view Redraw

Tvmain.nodes[feedindex]. Nodes.clear ();

foreach (article Atcl inpm. Profile.feeds[feedindex]. Articles.values)

{

TreeNode node;

Node =tvmain.nodes[feedindex]. Nodes.Add (ATCL. Title);

}

Enable tree view Redraw

Tvmain.endupdate ();

Select the article node to display the content in the WebBrowser

if (tvmain.selectednode.level== 0) return;

int Feedindex =tvmain.selectednode.parent.index;

String Articlekey =tvmain.selectednode.text;

Wbmain.navigate

(PM. Profile.feeds[feedindex]. Articles[articlekey]. URL);


C # 30 ADO and XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.