Tải bản đầy đủ (.pdf) (5 trang)

C Sharp và kiến trúc .NET. C Sharp cơ bản- P8 docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (131.57 KB, 5 trang )

XML in .NE
XML in .NE
T
T
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
7
7
Đ
Đ


c
c
XML
XML
public class Sample
public class Sample
{


{
static void
static void
Main(string
Main(string
[]
[]
args
args
)
)
{
{
XmlTextReader
XmlTextReader
textReader
textReader
= new
= new
XmlTextReader("C:
XmlTextReader("C:
\
\
\
\
books.xml
books.xml
");
");
textReader.Read

textReader.Read
();
();
while (
while (
textReader.Read
textReader.Read
() ) // If the node has value
() ) // If the node has value
{
{
// Move to fist element
// Move to fist element
textReader.MoveToElement
textReader.MoveToElement
();
();
Console.WriteLine("XmlTextReader
Console.WriteLine("XmlTextReader
Properties Test");
Properties Test");
Console.WriteLine
Console.WriteLine
("===================");
("===================");
// Read this element's properties and display them on co
// Read this element's properties and display them on co
nsole
nsole
Console.WriteLine("Name

Console.WriteLine("Name
:" +
:" +
textReader.Name
textReader.Name
);
);
Console.WriteLine("Base
Console.WriteLine("Base
URI:" +
URI:" +
textReader.BaseURI
textReader.BaseURI
);
);
Console.WriteLine("Local
Console.WriteLine("Local
Name:" +
Name:" +
textReader.LocalName
textReader.LocalName
);
);
Console.WriteLine("Attribute
Console.WriteLine("Attribute
Count:" +
Count:" +
textReader.AttributeCount.ToString
textReader.AttributeCount.ToString
());

());
Console.WriteLine("Depth
Console.WriteLine("Depth
:" +
:" +
textReader.Depth.ToString
textReader.Depth.ToString
());
());
Console.WriteLine("Line
Console.WriteLine("Line
Number:" +
Number:" +
textReader.LineNumber.ToString
textReader.LineNumber.ToString
());
());
Console.WriteLine("Node
Console.WriteLine("Node
Type:" +
Type:" +
textReader.NodeType.ToString
textReader.NodeType.ToString
());
());
Console.WriteLine("Attribute
Console.WriteLine("Attribute
Count:" +
Count:" +
textReader.Value.ToString

textReader.Value.ToString
());
());
}
}
}
}
}
}
XML in .NE
XML in .NE
T
T
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
8
8
Ghi

Ghi
XML
XML
Đ
Đ


ghi
ghi
XML,
XML,
d
d
ù
ù
ng
ng
XmlTextWriter
XmlTextWriter
.
.


H
H
à
à
m
m
t

t


o
o
v
v


i
i
tên
tên
file
file
c
c


n
n
ghi
ghi
.
.


C
C
á

á
c
c
phương
phương
th
th


c
c
quan
quan
tr
tr


ng
ng
.
.
WriteStartDocument
WriteStartDocument
():
():
ghi
ghi
ph
ph



n
n
khai
khai
b
b
á
á
o
o
version XML.
version XML.
WriteStartElement(string
WriteStartElement(string
):
):
ghi
ghi
th
th


m
m


đ
đ



u
u
c
c


a
a
m
m


t
t
n
n
ú
ú
t
t
.
.
WriteAttributeString(string
WriteAttributeString(string
, string):
, string):
ghi
ghi
thu

thu


c
c
t
t
í
í
nh
nh
v
v
à
à
gi
gi
á
á
tr
tr


c
c


a
a
n

n
ó
ó
.
.
WriteElementString(string
WriteElementString(string
, string):
, string):
ghi
ghi
m
m


t
t
n
n
ú
ú
t
t
,
,
trong
trong
đ
đ
ó

ó
c
c
ó
ó
ch
ch


a
a
m
m


t
t
gi
gi
á
á
tr
tr


.
.
WriteEndElement
WriteEndElement
():

():
ghi
ghi
th
th


k
k
ế
ế
t
t
th
th
ú
ú
c
c
c
c


a
a
m
m


t

t
n
n
ú
ú
t
t
.
.
XML in .NE
XML in .NE
T
T
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
9
9
Ghi

Ghi
XML
XML
public class Sample
public class Sample
{
{
public static void Main()
public static void Main()
{
{
XmlTextWriter
XmlTextWriter
writer = new
writer = new
XmlTextWriter("titles.xml
XmlTextWriter("titles.xml
", null);
", null);
//Write the root element
//Write the root element
writer.WriteStartElement("items
writer.WriteStartElement("items
");
");
//Write sub
//Write sub
-
-
elements

elements
writer.WriteElementString("title
writer.WriteElementString("title
", "Unreal Tournament 2003");
", "Unreal Tournament 2003");
writer.WriteElementString("title
writer.WriteElementString("title
", "C&C: Renegade");
", "C&C: Renegade");
writer.WriteElementString("title
writer.WriteElementString("title
", "Dr. Seuss's ABC");
", "Dr. Seuss's ABC");
// end the root element
// end the root element
writer.WriteEndElement
writer.WriteEndElement
();
();
//Write the XML to file and close the writer
//Write the XML to file and close the writer
writer.Close
writer.Close
();
();
}
}
}
}
XML in .NE

XML in .NE
T
T
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang
n Quang
Minh
Minh
10
10
DOM
DOM
trong
trong
.NET
.NET
DOM (Document Object Model):
DOM (Document Object Model):


h

h
ì
ì
nh
nh
đ
đ


i
i




ng
ng
t
t
à
à
i
i
li
li


u
u
cho

cho
ph
ph
é
é
p
p
x
x




XML
XML
m
m


t
t
c
c
á
á
ch
ch
m
m



m
m
d
d


o
o


Kh
Kh
á
á
c
c
v
v


i
i
XmlTextReader
XmlTextReader
,
,
XmlTextWriter
XmlTextWriter
ch

ch


cho
cho
ph
ph
é
é
p
p
đ
đ


c
c
v
v
à
à
ghi
ghi
XML
XML
theo
theo
ki
ki



u
u
tu
tu


n
n
t
t


, DOM
, DOM
cho
cho
ph
ph
é
é
p
p
truy
truy
c
c


p

p
ng
ng


u
u
nhiên
nhiên
v
v
à
à
o
o
t
t
à
à
i
i
li
li


u
u
XML.
XML.



C
C
á
á
c
c
l
l


p
p
quan
quan
tr
tr


ng
ng
XmlDocument
XmlDocument
:
:
cho
cho
ph
ph
é

é
p
p
x
x




XML
XML
theo
theo
DOM
DOM
XmlNodeList
XmlNodeList
:
:
danh
danh
s
s
á
á
ch
ch
c
c
á

á
c
c
node
node
trong
trong
t
t
à
à
i
i
li
li


u
u
XML.
XML.
XmlNode
XmlNode
:
:
m
m


t

t
node
node
đơn
đơn
nh
nh


t
t
trong
trong
t
t
à
à
i
i
li
li


u
u


XmlDocument
XmlDocument
cho

cho
ph
ph
é
é
p
p
thêm
thêm
node
node
m
m


i
i
,
,
n
n


i
i
node
node
v
v
à

à
o
o
đuôi
đuôi
t
t
à
à
i
i
li
li


u
u
,
,
xo
xo
á
á
node
node
kh
kh


i

i
t
t
à
à
i
i
li
li


u
u
.
.
XML in .NE
XML in .NE
T
T
-
-
Editor
Editor
:
:
Đo
Đo
à
à
n Quang

n Quang
Minh
Minh
11
11
DOM
DOM
trong
trong
.NET
.NET
void Sample()
void Sample()
{
{
oXmlDoc
oXmlDoc
= new
= new
XmlDocument
XmlDocument
();
();
oXmlDoc.Load(Server.MapPath("xml_XmlDocument.xml
oXmlDoc.Load(Server.MapPath("xml_XmlDocument.xml
"));
"));
XmlNode
XmlNode
oNode

oNode
=
=
oXmlDoc.DocumentElement
oXmlDoc.DocumentElement
;
;
Console.WriteLine("Node
Console.WriteLine("Node
Name: " +
Name: " +
oNode.Name
oNode.Name
);
);
XmlNodeList
XmlNodeList
oNodeList
oNodeList
=
=
oNode.SelectNodes
oNode.SelectNodes
("/books/category/title");
("/books/category/title");
Console.WriteLine("NodeList
Console.WriteLine("NodeList
count=" +
count=" +
oNodeList.Count

oNodeList.Count
);
);
for(int
for(int
x = 0; x <
x = 0; x <
oNodeList.Count
oNodeList.Count
; x++)
; x++)
Console.WriteLine("NodeList
Console.WriteLine("NodeList
Item#" + x + " " +
Item#" + x + " " +
oNodeList.Item[x].InnerText
oNodeList.Item[x].InnerText
);
);
}
}

×