Working with XML in .NET

1 / 87
About This Presentation
Title:

Working with XML in .NET

Description:

country:name Bulgaria /country:name /town:town /country:towns ... ??? ? ??? 'name' ?? ?????????????? 'country', ?.?. ??????? ??? ?? ???? ? 'urn:nakov-com: ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 88
Provided by: Svetli6

less

Transcript and Presenter's Notes

Title: Working with XML in .NET


1
(No Transcript)
2
?????? ? XML
???????????? ?? .NET Framework
http//www.nakov.com/dotnet/
??????? ?????
?????????? ???????? ?? ?????????? ?? ???????
academy.devbg.org
3
?????????? ??????
  • ?????? ???????? ?? .NET Framework
  • ?????? ???????? ?? ????? C
  • ?????? ???????? ?? ????? XML ? ?????????? ? ????
    ??????????

4
?????????? (1)
  • ????? ? XML?
  • XML ? HTML
  • ???? ?? ???????? XML?
  • ???????????? ?? ?????
  • ????? ? ????????? DTD, XSD ? XDR ?????
  • ?????????? ?? ????? ?? VS.NET
  • XML ???????
  • XML ????????? ? .NET Framework
  • ?????? ? DOM ??????? ????????? XmlNode ?
    XmlDocument

5
?????????? (2)
  • SAX ??????? ? ?????? XmlReader
  • ???? ?? ?????????? DOM ? ???? SAX?
  • ????????? ?? XML ????????? ? XmlWriter
  • ????????? ?? XML ?? ?????
  • ?????? ? XPath ????????? XPathNavigator ?
    XPathDocument
  • XSL ????????????? ? .NET Framework

6
????? ? XML?
  • XML ?
  • ??????????? ???? (???????) ?? ???????? ??
    ????????????? ?????
  • ??????? ?? ?????????? ?????? ? ????-?????????? ??
    ???
  • ??????? ?? HTML ????????-???????, ????????
    ?????? ? ????????
  • ? ???? ?? ??????? ????? ????? (???????) ??
    ??????????? ?? ?????
  • ????????-???????? ????????, ????????? ?? W3C
    (www.w3c.org)
  • ????????? ?? ???????????, ??????? ?? ????????????
    ? ????????????? ???????

7
XML ??????
lt?xml version"1.0"?gt ltlibrary name".NET
Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
8
XML ??????
???????? ???? (??????)
???????
lt?xml version"1.0"?gt ltlibrary name".NET
Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
??????? ???
???????
???????? ???
???????? ?? ???????
9
XML ? HTML
  • ??????? ????? ??????? XML ? HTML
  • ? ????? ?? ???????? ????????
  • ????????? ?????? ? ????????
  • ??????? ????? ??????? XML ? HTML
  • HTML ? ????, ? XML ? ????????? ?? ???????? ??
    ????? ?????
  • HTML ?????? ????????????? ?? ????????????, ? XML
    ?????? ????????????? ??????????
  • XML ??????? ??????????? ?? ?? ????? ??????????
    (well-formed)

10
????? ?????????? ?????????
  • XML ??????? ??????????? ?? ?? ????? ??????????
    (well-formed)
  • ???????? ?????? ?? ?? ???????? ? ?? ? ?????????
    ??? (?? ?? ?? ?????????)
  • ?????????? ?????? ?? ?? ????????
  • ?? ??? ???? ???? ??????? root ???????
  • ??????????? ????? ??????? ?? ???????? ?
    ??????????
  • ?????? ?? ????-????????? ????????

ltxmlgt ltbutton bug! value"OK name"b1"gt
ltanimation source"demo1.avi"gt 1 lt 2 lt 3
lt/click-buttongt lt / xml gt
11
???? ?? ???????? XML?
  • XML ?? ????????
  • ?? ?????? ?? ?????????? ????? ???????? ???????
  • ?? ?????????? ?? ????????????? ?????
  • ?? ????????? ?? ????????? ????? ?? ???????? ??
    ??????????
  • ??????????? ?? XML
  • ??????? ?? ??-????????
  • ???????? ????????????????
  • ????? ???? ? ?????????? ????? ?????
  • ??????????? ?? ???????? ??????

12
???????????? ?? ?????
  • ?????????????? ?? ????? (namespaces) ? XML
    ??????????? ?????????? ?????????? ? ?????????? ??
    ?????? ? ??????? ?????

lt?xml version"1.0" encoding"UTF-8"?gt ltcountryto
wns xmlnscountry"urnnakov-comcountry"
xmlnstown"http//www.nakov.com/town"gt
lttowntowngt lttownnamegtSofialt/townnamegt
lttownpopulationgt1 200 000lt/townpopulation
gt ltcountrynamegtBulgarialt/countrynamegt
lt/towntowngt lttowntowngt
lttownnamegtPlovdivlt/townnamegt
lttownpopulationgt700 000lt/townpopulationgt
ltcountrynamegtBulgarialt/countrynamegt
lt/towntowngt lt/countrytownsgt
13
???????????? ?? ?????
lt?xml version"1.0" encoding"UTF-8"?gt ltcountryto
wns xmlnscountry"urnnakov-comcountry"
xmlnstown"http//www.nakov.com/town"gt
lttowntowngt lttownnamegtSofialt/townnamegt
lttownpopulationgt1 200 000lt/townpopulation
gt ltcountrynamegtBulgarialt/countrynamegt
lt/towntowngt lttowntowngt
lttownnamegtPlovdivlt/townnamegt
lttownpopulationgt700 000lt/townpopulationgt
ltcountrynamegtBulgarialt/countrynamegt
lt/towntowngt lt/countrytownsgt
???????????? ? ??????? "country" ? URI
????????????? "urnnakov-comcountry"
??? ? ??? "name" ?? ?????????????? "country",
?.?. ??????? ??? ?? ???? ? "urnnakov-comcountry
name"
14
???????????? ?? ?????
  • ????????? ?? ???????????? ?? ???????????? ??
    ????????????

lt?xml version"1.0" encoding"windows-1251"?gt ltord
er xmlns"http//www.hranitelni-stoki.com/orders"gt
ltitemgt ltnamegt???? "???????"lt/namegt
ltammountgt8lt/ammountgt
ltmeasuregt???????lt/measuregt
ltpricegt3.76lt/pricegt lt/itemgt ltitemgt
ltnamegt?????????lt/namegt
ltammountgt12lt/ammountgt ltmeasuregt????lt/measu
regt ltpricegt4.20lt/pricegt
lt/itemgt lt/ordergt
???????????? ?? ????????????
??????? ??? ?? ???? "item" ? "http//www.hraniteln
ik-stoki.comitem"
15
????? ? ?????????
  • ??????????? ?? XML ??????????? ?? ?????? ???
    ?????
  • ??????? ???????
  • ??????????? ??????
  • ??????????? ???????? ?? ????????
  • ??????????? ????????? ?? ?????????? ? ??
    ??????????
  • ???? ?? ????????? ?? ????????
  • ????????? ?? ????????????
  • ??? ??????? ????????? ?? XML ?????
  • DTD Document Type Definition
  • XSD XML Schema Definition Language
  • XDR XML-Data Reduced

16
?????? DTD
  • DTD (Document Type Definition) ?
  • ???????? ???? ?? ????a??? ??????????? ?? XML
    ?????????
  • ??????? ?????????? ?? ??????? ?? ???????? ?
    ????????? ? ??????? ??????????
  • ????????-??????? ????, ?? ?? ? ??????? ?? XML
  • ???????? ?? ?????, ?????? ? ???????? ?? XSD
  • ??????

lt!ELEMENT library (book)gt lt!ATTLIST library
name CDATA REQUIRED gt lt!ELEMENT book (title,
author, isbn)gt lt!ELEMENT title (PCDATA)gt lt!ELEMEN
T author (PCDATA)gt lt!ELEMENT isbn (PCDATA)gt
17
XSD ?????
  • XSD (XML Scheme Definition Language) ?
  • ????? XML-??????? ???? ?? ???????? ??????????? ??
    XML ?????????
  • ??????? ?????????? ?? ??????? ?? ???????? ?
    ????????? ? ??????? ????????

lt?xml version"1.0" encoding"UTF-8"?gt ltxsschema
xmlnsxs"http//www.w3.org/2001/XMLSchema"gt
ltxselement name"library"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"book"
maxOccurs"unbounded"/gt
lt/xssequencegt ltxsattribute
name"name" type"xsstring"
use"optional"/gt lt/xscomplexTypegt
lt/xselementgt (???????? ??????????)
18
XSD ?????
ltxselement name"book"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"title"/gt
ltxselement ref"author"/gt
ltxselement ref"isbn"/gt
lt/xssequencegt lt/xscomplexTypegt
lt/xselementgt ltxselement name"title"
type"xsstring"/gt ltxselement name"author"
type"xsstring"/gt ltxselement name"isbn"
type"xsstring"/gt lt/xsschemagt
  • XSD ??????? ???? ??-?????? ??????????? ??? ?? DTD
  • XSD ?????????? ???????? DTD

19
XDR ?????
  • XDR (XML-Data Reduced) ?
  • ???? ?? ???????? ?? ??????????? ?? XML ?????????
  • ????????? ??????? ?? XML-Data ???????
  • XML ??????? ????, ??????? ?? XSD
  • ??-????? ?? DTD
  • ??-????? ??????????? ?? XSD
  • ??????? ?? Microsoft, ????????? ????? ? ?????
    ???????? ? ?????????? (????. ? SQL Server 2000,
    BizTalk, )
  • ???? ?? ?????? ???????????? ????? ??????????? ??
    XML ????????? ? ?????????? ???? ?????

20
XDR ????? ??????
lt?xml version"1.0" encoding"UTF-8"?gt ltSchema
xmlns"urnschemas-microsoft-comxml-data"
xmlnsdt"urnschemas-microsoft-comdatatypes"gt
ltElementType name"author" model"closed"
content"textOnly" dttype"string"/gt
ltElementType name"title" model"closed"
content"textOnly" dttype"string"/gt
ltElementType name"isbn" model"closed"
content"textOnly" dttype"string"/gt
ltElementType name"book" model"closed"
content"eltOnly" order"seq"gt ltelement
type"title" minOccurs"1" maxOccurs"1"/gt
ltelement type"author" minOccurs"1"
maxOccurs"1"/gt ltelement type"isbn"
minOccurs"1" maxOccurs"1"/gt
lt/ElementTypegt (???????? ??????????)
21
XDR ????? ??????
ltElementType name"library" model"closed"
content"eltOnly" order"seq"gt
ltAttributeType name"name" dttype"string"
required"yes"/gt ltattribute type"name"/gt
ltAttributeType name"xmlns" dttype"string"/gt
ltattribute type"xmlns"/gt ltelement
type"book" minOccurs"1" maxOccurs""/gt
lt/ElementTypegt lt/Schemagt
22
?????????? ?? ????? ?? VS.NET
  • VS.NET ???? ?? ??????? XSD ????? ?? ???????????
    ?? ????? XML ????????
  • VS.NET ??? ???????? ?? XSD ?????
  • ? ???? ????? ?? ??????????? ???????? ???????
  • ????? ?? ?????????, ????????? ? ?????????
  • ????????
  • ???????? ?? ????????
  • ??????

23
???????????? 0
  • ?????????? ?? VS.NET ?? ????????? ? ???????????
    ?? XSD ?????

24
XML ???????
  • XML ????????? ?? ????????? ??????????, ?????
    ????????? ???????? ? XML
  • ?????? ??
  • ????????? ?? ????? ?? XML ?????????
  • ??????????? ?? XML ?????????
  • ????????? ?? XML ????????? ?? ?????? ?????
  • ?? ????? ?? ?????? ?????
  • DOM (Document Object Model) ????????? XML
    ??????????? ???? ????? ? ??????? ? ?????????
    ????? ?????????
  • SAX (Simple API for XML Processing) ???? XML
    ??????????? ?????????????? ???? ????? ? ?????????
    ??????????? ?? ???????????? ??

25
XML ? .NET Framework
  • ? .NET Framework
  • ?????????? ?? ?????? ? XML ?? ??????? ?
    ?????????????? System.Xml
  • ??? ????? ?????????? ?? DOM ??????
  • ???? ????????? XmlDocument, XmlNode,
  • XML ?????????? ?? ??????? ?????? ???? ????? ?
    ??????? ? ???? ???? ?? ?????????
  • ??? ???????, ? ?????????????? ??????? ?? SAX, ??
    ???? ????? SAX ?????????????
  • ????????? XmlReader ? XmlWriter ????? ? ????? XML
    ????????? ?????????????? ??????? ?? ???????

26
?????? ? DOM ???????
  • ????? ? ??????? XML ????????

lt?xml version"1.0"?gt ltlibrary name".NET
Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
27
?????? ? DOM ???????
  • ???? ???????? ?? ????????? ? ??????? ???? DOM
    ????? ?? ??????? ?????

??????? ??????? (root node)
???????? ????
28
DOM ?????? ??????
XmlDocument doc new XmlDocument() doc.Load("lib
rary.xml") XmlNode rootNode doc.DocumentElement
Console.WriteLine("Root node 0",
rootNode.Name) foreach (XmlAttribute atr in
rootNode.Attributes) Console.WriteLine("Attr
ibute 01", atr.Name,
atr.Value) foreach (XmlNode node in
rootNode.ChildNodes) Console.WriteLine("\nBo
ok title 0", node"title".InnerText)
Console.WriteLine("Book author 0",
node"author".InnerText)
Console.WriteLine("Book isbn 0",
node"isbn".InnerText)
29
???????????? 1
  • ???????? ?? XML ???????? ? DOM

30
????????? ?? ?????? ? DOM
  • ?? ?????? ? DOM ?? ????????? ?????????
  • XmlNode ?????????? ????? ???? ?? ?????? ????? ?
    ???? DOM ?????
  • XmlDocument ??????????? ?? ?????? ?? DOM ?????,
    ?????????? ??????? ??? ??????????
  • ???????? ???? (??????) ?? XML ?????????
  • ???????-????? ?? XML ?????????
  • XmlElement ????????? XML ???????
  • XmlAttribute ????????? ??????? ?? XML ???????
    (?????? ???-????????)
  • XmlAttributeCollection ?????? ?? XML ????????
  • XmlNodeList ?????? ?? ????? ? DOM ?????

31
?????? XmlNode
  • ?????? System.Xml.XmlNode
  • ? ????????????? ??? ???????? ? DOM
  • ???????????? ????? ?????, ? ???????? ??????????
    ?? ???????? DOM ?????
  • XmlDocument, XmlElement, XmlAttribute,
  • ????????? ????????? ? DOM ???????
  • ParentNode ????? ??????-??????? (??? null ???
    ????)
  • PreviousSibling / NextSibling ????? ????? /
    ?????? ????? ?? ??????? ?????
  • FirstChild / LastChild ????? ?????? / ?????????
    ????????? ?? ??????? ?????
  • Item (?????????? ? C) ????? ????????? ??
    ??????? ????? ?? ????? ??

32
?????? XmlNode
  • ?????? System.Xml.XmlNode
  • ????????? ?????? ? ??????? ?????
  • Name ????? ????? ?? ?????? (??? ?? ???????,
    ???????, ...)
  • Value ?????????? ?? ??????
  • Attributes ?????? ?? ?????????? ?? ?????? (????
    XmlAttributeCollection)
  • HasChildNodes ???? ??? ??????????
  • InnerXml, OuterXml ????? ?????? ?? XML
    ?????????, ????? ?????? ???????????? ?? ??????
    ????????? ? ? ??? ???? ?????
  • InnerText ???????????? ?? ??????????? ?? ??????
    ? ???????????? ?? ??????????
  • NodeType ????? ???? ?? ?????? (??. ?????????
    ??? XmlNodeType ? MSDN)

33
?????? XmlNode
  • ?????? System.Xml.XmlNode
  • ????????? ??????? ?? ??????? ?????
  • AppendChild() / PrependChild() ?????? ???
    ????????? ???? / ????? ?????? ????? ?????????? ??
    ??????? ?????
  • InsertBefore() / InsertAfter() ?????? ???
    ????????? ????? / ???? ?????? ?????????
  • RemoveChild() / ReplaceChild() ???????? /
    ?????? ???????? ?????????
  • RemoveAll() ??????? ?????? ?????????? ??
    ??????? ????? (????????, ????????, ...)
  • Value, InnerText, InnerXml ??????? ?????????? /
    ?????? / XML ?????? ?? ??????

34
?????? XmlDocument
  • ?????? System.Xml.XmlDocument
  • ??????? XML ???????? ??? ??? ?? DOM ?????
  • ????????? ????????? ? ?????????? ?? XML ?????????
    ??/??? ????, ????? ??? ???????? ??? (??.
    Load(), LoadXml(), Save())
  • ??-????? ????????, ?????? ? ???????
  • DocumentElement ??????? ????????-?????
  • PreserveWhitespace ?????? ???? ????????
    ???????????? ?? ???? ???????? ???
    ?????????/????????? ?? ?????????
  • CreateElement(), CreateAttribute(),
    CreateTextNode() ??????? ??? XML ???????,
    ??????? ??? ???????? ?? ???????
  • NodeChanged, NodeInserted, NodeRemoved ???????
    ?? ??????? ?? ??????? ? ?????????

35
?????? ? DOM ??????
  • ????????? ?? ?????? ?? ?????? ? ??????? XML
    ????????

lt?xml version"1.0" encoding"windows-1251"?gt ltite
ms culture"en-US"gt ltitem type"beer"gt
ltnamegt???????lt/namegt ltpricegt0.54lt/pricegt
lt/itemgt ltitem type"food"gt
ltnamegt?????????lt/namegt ltpricegt0.48lt/pricegt
lt/itemgt ltitem type"beer"gt
ltnamegt????????lt/namegt ltpricegt0.56lt/pricegt
lt/itemgt lt/itemsgt
36
?????? ? DOM ??????
static void Main() XmlDocument doc new
XmlDocument().Load("items.xml") string
culture doc.DocumentElement.
Attributes"culture".Value CultureInfo
numberFormat new CultureInfo(culture)
foreach (XmlNode node in doc.DocumentElement)
if (node.Attributes"type".Value
"beer") string
currentPriceStr
node"price".InnerText decimal
currentPrice Decimal.Parse(
currentPriceStr, numberFormat)
decimal newPrice currentPrice 2
node"price".InnerText
newPrice.ToString(numberFormat)
doc.Save("itemsNew.xml")
37
???????????? 2
  • ??????? ?? XML ???????? ? DOM

38
SAX ??????? ? XmlReader
  • ? .NET Framework ???? ????????????? ?? ??????????
    SAX ??????
  • ???????????? SAX ???????
  • ???????? ????????? ??????????????
  • ???????? callback ??????? ??? ??????? ??
    ?????????? ?????
  • ? .NET ???????????????? ????????? ?? XML
    ????????? ?? ???????? ? XmlReader
  • XmlReader ? ?????????? ????, ?????
  • ?????????? ??????????? ?????? ???? ?? ?????? ??
    XML ?????
  • ?????? ???? ?????, ?? ???? XML ?????????
  • ??????????? ?? ????? ?????? ????? ????? ?? ??
    ???????? ? ?????????? ?? ???????????

39
?????? XmlReader
  • ?????? XmlReader ?????????? ???????? ?????? ?
    ????????
  • Read() ??????? ????????? ????? ?? XML ?????????
    ??? ????? false ??? ???? ???????
  • NodeType ????? ???? ?? ?????????? ?????
  • Name ????? ????? ?? ?????????? ????? (??? ??
    ???????, ?? ???????, ...)
  • HasValue ????? ???? ??????? ??? ????????
  • Value ????? ?????????? ?? ??????
  • ReadElementString() ??????? ?????????? (??????)
    ?? ????????? ???????? ???????
  • AttributeCount, GetAttribute() ?? ????????? ??
    ?????????? ?? XML ???????

40
?????? XmlReader
  • XmlReader ? ?????????? ????
  • ?? ?????? ? ???? ?? ????????? ????????
    ??????????
  • XmlTextReader ?? ?????? ?? ???? ??? ?????
  • XmlNodeReader ?? ?????? ?? ????? ? DOM ?????
  • XmlValidatingReader ?? ????????? ?? XSD, DTD
    ??? XDR ????? ??? ?????? ?? ???? XmlReader
  • ????? ?? ??????????

XmlTextReader reader new XmlTextReader("some-fil
e.xml") while (reader.Read()) // Analyze
the read node
41
XmlReader ??????
  • ????? ? XML ????????, ???????? ?????? ?? ?????

lt?xml version"1.0"?gt ltlibrary name".NET
Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
42
XmlReader ??????
  • ????????? ?? ?????? ????????
  • ????????? ?? ??????? ?? ?????? ????????

XmlTextReader reader new XmlTextReader("library.
xml") while (reader.Read()) if
((reader.NodeType XmlNodeType.Element)
(reader.Name "title"))
Console.WriteLine(reader.ReadElementString())

XmlTextReader reader new XmlTextReader("library.
xml") while (reader.Read()) if
(reader.NodeType XmlNodeType.Element)
Console.WriteLine(reader.Name)
43
???????????? 3
  • ?????? ? XmlReader

44
???? ?? ?????????? DOM ? SAX?
  • ??????? ?? ????????? ?? XML ????????? DOM
    (XmlDocument) ? ???????? ??????
  • ??????????? ????? ?? ???? ?????????
  • ??????? ?? ?? ????????? ??? ???????????
  • ????? ????? ?? ???? ?????? ?? ????????? ????? ??
    ?????????
  • ?????? ?? ????????? ?????????
  • ??????? ?? ????????? ?? XML ????????? SAX
    (XmlReader) ? ???????? ??????
  • ??????????? ?????? ?? ???? ?????????
  • ????????? ?? ????????? ? ?????
  • ?? ? ?????????? ?? ????????? ??????? ?? ?????????

45
?????? ? XmlWriter
  • ?????? XmlWriter ????????? ????????? ?? XML
    ?????????
  • ?????? ???? ?????, ?? ???? ? XML ?????????
  • XmlWriter ???????? ???????? ??????
  • WriteStartDocument() ?????? ?????? ?????? ?
    ???????? ?? ????????? (lt?xml )
  • WriteStartElement() ?????? ??????? ???
  • WriteEndElement() ??????? ????????? ???
  • WriteAttributeString() ?????? ??????? ?
    ??????? ???????
  • WriteElementString() ?????? ??????? ??
    ???????? ??? ? ???????? ????????
  • WriteEndDocument() ??????? ?????? ?????? ?
    ???????? ?????????? ?????? (???? Flush())

46
?????? ? XmlWriter
  • XmlWriter ? ?????????? ???? ? ?? ?? ??????????
    ????????
  • XmlTextWriter ???? XML ??? ??????? ? ??????
  • ??? ???????????? ?????? ? ????????
  • Format, Indentation, IndentChar ???????
    ??????????? ?? ?????????? ??????? ?? ?????????
    ??????
  • QuoteChar ?????? ??????? ?? ???????? ??
    ??????????? ?? ??????????
  • ? ???????????? ???? ?? ?? ?????? ??????????,
    ????? ?? ?? ????????
  • ?? ???????????? ?? ???????? UTF-8

47
?????? ? XmlWriter ??????
public static void Main() XmlTextWriter
writer new XmlTextWriter("lib.xml",
Encoding.GetEncoding("windows-1251"))
writer.Formatting Formatting.Indented
writer.IndentChar '\t' writer.Indentation
1 try writer.WriteStartDocume
nt() writer.WriteStartElement("library")
writer.WriteAttributeString("name", "My
Library") WriteBook(writer, "Code
Complete", "Steve McConnell",
"155-615-484-4") WriteBook(writer,
"???????? ???????????? ? Java",
"??????? ?????", "954-775-305-3")
WriteBook(writer, "Writing Solid Code",
"Steve Maguire", "155-615-551-4")
writer.WriteEndDocument() (???????? ??????????)
48
?????? ? XmlWriter ??????
finally writer.Close()
private static void WriteBook(XmlWriter
aWriter, string aTitle, string aAuthor,
string aIsbn) aWriter.WriteStartElement("boo
k") aWriter.WriteElementString("title",
aTitle) aWriter.WriteElementString("author",
aAuthor) aWriter.WriteElementString("isbn",
aIsbn) aWriter.WriteEndElement()
49
?????? ? XmlWriter ??????
  • ?????????? XML ????????

lt?xml version"1.0" encoding"windows-1251"?gt ltlib
rary name"My Library"gt ltbookgt
lttitlegtCode Completelt/titlegt
ltauthorgtSteve McConnelllt/authorgt
ltisbngt155-615-484-4lt/isbngt lt/bookgt
ltbookgt lttitlegt???????? ???????????? ?
Javalt/titlegt ltauthorgt???????
?????lt/authorgt ltisbngt954-775-305-3lt/isbngt
lt/bookgt ltbookgt lttitlegtWriting
Solid Codelt/titlegt ltauthorgtSteve
Maguirelt/authorgt ltisbngt155-615-551-4lt/isbn
gt lt/bookgt lt/librarygt
50
???????????? 4
  • ?????? ? XmlWriter

51
????????? ?? XML ?? ?????
  • ??????? (XSD, DTD ? XDR) ??????? ??????? ?
    ??????????? ?? ????????? ?? XML ?????????
  • ??????????? ?????? ? ?????? ?? ???????
  • ??????????? ???????? ? ?????????
  • ???? ?? ????????
  • ??????????? ?? XML ???????? ?? ?????? ????? ?
    .NET Framework ?? ???????? ? ?????????? ???????
  • ???????? ?? ?????? XmlValidatingReader
  • ???????? ?? XSD, DTD ? XDR
  • ???????? ?? ???????? (?? ?? ?? ?? ?????? ???????
    ?? ???????? ??? ?? ??????? ???????)

52
????????? ?? XML ??????
  • ????? ??????? XML ????

lt?xml version"1.0"?gt ltlibrary xmlns"http//www.n
akov.com/schemas/library" xmlnsxsi"http//www.
w3.org/2001/XMLSchema-instance"
xsischemaLocation"http//www.nakov.com/schemas/l
ibrary http//www.nakov.com/schemas/library.xs
d" name".NET Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
53
????????? ?? XML ??????
  • ? ???? ? ??????? ???????????? XSD ?????

lt?xml version"1.0"?gt ltlibrary xmlns"http//www.n
akov.com/schemas/library" xmlnsxsi"http//www.
w3.org/2001/XMLSchema-instance"
xsischemaLocation"http//www.nakov.com/schemas/l
ibrary http//www.nakov.com/schemas/library.xs
d" name".NET Developer's Library"gt ltbookgt
lttitlegtProgramming Microsoft .NETlt/titlegt
ltauthorgtJeff Prosiselt/authorgt
ltisbngt0-7356-1376-1lt/isbngt lt/bookgt
ltbookgt lttitlegtMicrosoft .NET for
Programmerslt/titlegt ltauthorgtFergal
Grimeslt/authorgt ltisbngt1-930110-19-7lt/isbngt
lt/bookgt lt/librarygt
URI ?? ???????????? ???????????? ?? ?????
URL ????? (? ????????), ?????? ? ??????????? XSD
???????
54
????????? ?? XML ??????
  • XSD ???????, ?? ????? ?? ??????????

lt?xml version"1.0" encoding"UTF-8"?gt ltxsschema
xmlnsxs"http//www.w3.org/2001/XMLSchema"
xmlns"http//www.nakov.com/schemas/library"
targetNamespace"http//www.nakov.com/schemas/libr
ary"gt ltxselement name"library"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"book"
maxOccurs"unbounded"/gt
lt/xssequencegt ltxsattribute
name"name" type"xsstring"
use"optional"/gt lt/xscomplexTypegt
lt/xselementgt (???????? ??????????)
??????? ?, ?? ??????? ?????? ?????????? ??
?????????????? http//www.nakov.com/schemas/librar
y
55
????????? ?? XML ??????
  • XSD ???????, ?? ????? ?? ??????????

ltxselement name"book"gt
ltxscomplexTypegt ltxssequencegt
ltxselement ref"title"/gt
ltxselement ref"author"/gt
ltxselement ref"isbn"/gt
lt/xssequencegt lt/xscomplexTypegt
lt/xselementgt ltxselement name"title"
type"xsstring"/gt ltxselement name"author"
type"xsstring"/gt ltxselement name"isbn"
type"xsstring"/gt lt/xsschemagt
56
????????? ?? XML ??????
  • ?????, ????? ???????? XML ?????????

using System using System.Xml using
System.Xml.Schema static void Main()
XmlTextReader tr new XmlTextReader("library.xml"
) XmlValidatingReader vr new
XmlValidatingReader(tr) vr.Schemas.Add("http
//www.nakov.com/schemas/library",
"library.xsd") vr.ValidationType
ValidationType.Schema vr.ValidationEventHandl
er new ValidationEventHandler
(ValidationHandler) mValid true (????????
??????????)
57
????????? ?? XML ??????
  • ?????, ????? ???????? XML ?????????

while(vr.Read()) // Do nothing,
just read the whole document. if
(mValid) Console.WriteLine("The
document is valid.") public static void
ValidationHandler( object aSender,
ValidationEventArgs aArgs) mValid false
Console.WriteLine("Validation error")
Console.WriteLine("\tSeverity0",
aArgs.Severity) Console.WriteLine("\tMessage
0", aArgs.Message)
58
???????????? 5
  • ????????? ?? XML ???????? ?? ?????? XSD ?????

59
??? ?? XML ???????????
  • ??????????? ???? ?? ?? ???????? ? ?? ????? ??
    ????????? ?? DOM ?????
  • ??? ????????? ?? DTD ????? ?? ??????? DTD ????
    ?????? ?? ?? ???????? XmlResolver ?? ????????? ??
    ???????? XML ???????
  • ??????????????? ????????? ? ???????? ?? ??????? ?
    XmlValidatingReader.Schemas ? ???????? ???? ??
    XSD ? XDR ?????

XmlDocument doc new XmlDocument() XmlTextReader
tr new XmlTextReader("Sample.xml") XmlValidati
ngReader valReader new XmlValidatingReader(
tr) valReader.ValidationType
valReader.ValidationEventHandler
doc.Load(valReader)
60
XPath
  • XPath (XML Path Language) e ???? ?? ?????????? ??
    ????? ?? XML ?????????
  • XPath ???????? ???????? ???????? ?? ?????? ??
    ????? ? ????????, ?? ????? ??????? ?????? ??
    ?????????
  • XPath ????? ???? ?? ???????? ?????? ? XSLT ?
    XPointer
  • ??????? ?? XPath ??????

/library/bookisbn'1-930110-19-7'
/catalog/cd_at_pricelt10.80
/book/chapter3/paralast()
61
XPath ????????
  • / ???????? ?????? ?? ?????????
  • /someNode ???????? ?????? ????? ? ???
    "someNode", ????? ?????????? ?? ??????
  • /books/book ???????? ?????? ????? "book",
    ?????????? ?? ?????? "books"
  • /books/bookpricelt"10"/author ???????? ??????
    ?????? (/books/book/ author), ????? ????? ????
    ???? lt "10"
  • /items/item_at_type"food" ???????? ?????? ?????
    ? ??? item, ????? ???? ??????? "type" ???
    ???????? "food" ? ?? ?????????? ?? ????? "items",
    ??????? ?? ?????? ?? ?????????

62
XPath ? .NET Framework
  • XPath ? ????? ?????? ?? ??????? ?? ?????????? ?
    XML ?????????
  • ????????? ? ???????????? ?? ?????? ? XPath ??
    ??????? ? ?????????????? System.Xml.XPath
  • XPath ???? ?? ?? ?????? ???????? ?? ????? XmlNode
    (? ?????? ?????? ??????????) ???? ????????
  • SelectNodes(string xPathQuery) ????? ?????? ??
    ?????? ?????, ????? ???????????? ?? ?????????
    XPath ?????
  • SelectSingleNode(string xPathQuery) ?????
    ?????? ?????, ????? ??????????? ?? ?????????
    XPath ?????

63
XPath ? XmlNode ??????
  • ????? ? ??????? XML ????????

lt?xml version"1.0" encoding"windows-1251"?gt ltite
msgt ltitem type"beer"gt
ltnamegt???????lt/namegt ltpricegt0.54lt/pricegt
lt/itemgt ltitem type"food"gt
ltnamegt?????????lt/namegt ltpricegt0.48lt/pricegt
lt/itemgt ltitem type"beer"gt
ltnamegt????????lt/namegt ltpricegt0.56lt/pricegt
lt/itemgt lt/itemsgt
64
XPath ? XmlNode ??????
  • ?????? ? ???? ??????? ?? ?????? ????? ?? ???
    ????

lt?xml version"1.0" encoding"windows-1251"?gt ltite
msgt ltitem type"beer"gt
ltnamegt???????lt/namegt ltpricegt0.54lt/pricegt
lt/itemgt ltitem type"food"gt
ltnamegt?????????lt/namegt ltpricegt0.48lt/pricegt
lt/itemgt ltitem type"beer"gt
ltnamegt????????lt/namegt ltpricegt0.56lt/pricegt
lt/itemgt lt/itemsgt
65
XPath ? XmlNode ??????
  • ????? ?? ?????????? ??????? ???

using System using System.Xml class
TestXPath static void Main(string args)
XmlDocument xmlDoc new
XmlDocument() xmlDoc.Load("items.xml")
string query "/items/item_at_type'beer'/n
ame" XmlNodeList beerNamesList
xmlDoc.SelectNodes(query) foreach
(XmlNode beerName in beerNamesList)
Console.WriteLine(beerName.InnerText)

66
???????????? 6
  • ??????? ? XPath ? XML ????????? ????
    XmlNode.SelectNodes()

67
?????????????? System.Xml.XPath
  • XPathNavigator ????????? ????????? ??
    ??????????-???????????? ????? ? ?????????? ??
    XPath ?????? ????? ???
  • XPathNodeIterator ????????? ????????? ??
    ????????? ?? XPath ?????? ???????? ??
    XPathNavigator.Select()
  • IXPathNavigable ?????????, ? ????? ??????????
    XPathNavigator. ???????? ?? ?? ????????? XmlNode
    ? XPathDocument
  • XPathDocument ????????? ???????????????
    ??????????? ?????? ? XPath ? XSLT ? ????? ???? ??
    ?????? (?? ?????? XML ?????????)
  • XPathExpression ??????? ?????????? XPath ?????
    (??-????? ????????????????)

68
XPathNavigator ??????
  • ???????? ?????????? ?? ?????? ?? ?????? ? 20

using System using System.Xml using
System.Xml.XPath using System.Globalization clas
s XPathNavigatorDemo public const decimal
DISCOUNT (decimal) 0.20 static void
Main() XmlDocument xmlDoc new
XmlDocument() xmlDoc.Load("items.xml")
CultureInfo numberFormat new
CultureInfo("en-US") (???????? ??????????)
69
XPathNavigator ??????
XPathNavigator nav xmlDoc.CreateNavigato
r() string xPathQuery
"/items/item_at_type'beer'/price"
XPathNodeIterator iter nav.Select(xPathQuery)
while (iter.MoveNext())
XPathNavigator currentNode iter.Current
XmlNode xmlNode
((IHasXmlNode) currentNode).GetNode()
string priceStr xmlNode.InnerText
decimal price
Decimal.Parse(priceStr, numberFormat)
price price (1 - DISCOUNT)
xmlNode.InnerText
price.ToString(numberFormat)
xmlDoc.Save("itemsNew.xml")
70
???????????? 7
  • ????????? ?? XML ???????? ? XPathNavigator

71
XPathDocument ??????
using System using System.Xml using
System.Xml.XPath class XPathNavigatorDemo
static void Main() // Create fast
forward-only XPath processor
XPathDocument doc new XPathDocument(
"../../../xml-files/items.xml")
XPathNavigator nav doc.CreateNavigator()
XPathExpression expr nav.Compile(
"/items/item_at_type'beer'/name")
XPathNodeIterator iter nav.Select(expr)
while (iter.MoveNext())
XPathNavigator currentNode iter.Current
Console.WriteLine(currentNode.Value)

72
XSL ?????????????
  • XSL ??????????????? (XSLT) ??????????
    ????????????? ?? ???? XML ???????? ? ???? XML
    ???????? ? ???????? ?????????
  • XSLT ??????? ?? XPath ?? ????????? ?? ????? ??
    ??????? ????????, ????? ?? ????????? ? ????????
    ????????
  • ? ???????? XSLT ???? ?? ?? ???????? ? ??
    ????????????? ?? XML ????????? ? XHTML
  • ???????? XSL ?????? ?? ?????????????

lt?xml version"1.0" encoding"windows-1251"?gt ltxsl
stylesheet version"1.0" xmlnsxsl"http//ww
w.w3.org/1999/XSL/Transform"gt ltxsltemplate
match"/"gt (???????? ??????????)
73
XSL ?????????????
lthtmlgt ltbodygt lth1gt????? ??????????lt/h1gt
lttable bgcolor"E0E0E0" cellspacing"1"gt lttr
bgcolor"EEEEEE"gt lttdgtltbgt????????lt/bgtlt/tdgt
lttdgtltbgt?????lt/bgtlt/tdgt lt/trgt
ltxslfor-each select"/library/book"gt lttr
bgcolor"white"gt lttdgtltxslvalue-of
select"title"/gtlt/tdgt lttdgtltxslvalue-of
select"author"/gtlt/tdgt lt/trgt
lt/xslfor-eachgt lt/tablegt lt/bodygt
lt/htmlgt lt/xsltemplategt lt/xslstylesheetgt
74
?????? XSL ???????????
  • ltxsltemplate match"XPath expr."gt
    lt/xsltemplategt ???????? ?????????? ? XPath
    ????? ???? ?? ????????? ? ?????? ?? ?????????????
  • ltxslfor-each select"XPath expr."gt
    lt/xslfor-eachgt ???????? ????? ?????, ?????????
    ?? ??????? XPath ????? ? ?????? ?? ?????????????
  • ltxslvalue-of select"XPath expr."/gt ???????
    ?????????? ?? ????????? XPath ????? (???? ???????
    ???????? ????????????)
  • ltxslsort select"XPath expr."/gt ?
    ltxslfor-each gt ??????????? ??????? ??
    ?????????? ?? ????? XPath ?????
  • XSL ????????? ?? ??????? XML ?????????!

75
XSLT ? .NET Framework
  • ? .NET Framework ????? ?? ?????????? XSL
    ????????????? ???? ????? System.Xml.Xsl.XslTransfo
    rm
  • ?????? XslTransform ???????? ????????
  • Load() ??????? XSL ??????, ?? ????? ?? ??
    ???????? ?????????????
  • Transform() ???????? ????????????? ?? ?????
    XML ????????
  • ?????? ???? ???? ??? ?? XML ????, XPathNavigator
    ??? IXPathNavigable
  • ??????? ?????? ? XML ????, ????? ??? XmlWriter

76
XSL ????????????? ??????
  • ????????????? ?? XML ???????? ?? ????? XSL ??????

using System using System.Xml.Xsl class
XSLTransformDemo static void Main()
XslTransform xslt new XslTransform()
xslt.Load("library-xml2html.xsl")
xslt.Transform("library.xml",
"library.html", null)
77
???????????? 8
  • ????????????? ?? XML ???????? ?? ????? XSL ??????

78
?????? ? XML
????????
79
??????????
  1. ????? ???????????? ?????? XML? ?? ????? ??????
    ???? ?? ?????????
  2. ???????? XML ???????? students.xml, ????? ???????
    ????????????? ???????? ?? ????????. ?? ?????
    ??????? ?????? ?? ??? ?????????? ?? ??????? ??,
    ???, ???????? ????, ?????, ???????, email, ????,
    ???????????, ?????????? ?????, ???, ????????,
    ???????? ?????? (??? ?? ?????, ????????????,
    ??????), ??????? ??????, ?????? ?????, ???? ??
    ???????? ??? ??? ? ???????? ???? ?? ??????????
    (?????? ? ?????).
  3. ????? ????????????? ?????????????? ?? ????? ? XML
    ???????????? ?? ????? ??????? ???? ?? ??????????

80
??????????
  1. ????????? ????? students.xml ? ?? ????????
    ???????????? ?? ????? ?? ????????????
    "urnstudents".
  2. ????? ????????????? XML ???????? ?? ????? ??
    ???????? ? ?? ????? ?? ?????????? DTD, XSD ? XDR
    ????????
  3. ? ??????? ?? VS.NET ???????? ????????? XSD ?????
    ?? ????????? ?? ????????? students.xml.
    ???????????? ???????????? ?????, ???? ??????????
    ?????????? ????? ???? ?? ?????????? ?? ????
    ??????? ?? ????? ??? ?????? ?? ????, ???? ??????
    ?? ? ???????????? ??? ?? ????? ? ????? ??
    ????????????? ??? ????????? ?? ?????????.

81
??????????
  1. ???? ????????? ?? VS.NET ??????????? XSD ????? ??
    ???????? ?? ????????? ???????. ????????? ??????
    ?? ??????? ?????????? ?? ?????? ?? ????????
    ???????????. ?? ????? ????? ?????? ?? ??
    ?????????? ????????????, ?????, ?????? ??
    ????????, ???????????? ????, ???? ? ?????? ??
    ???????. ????? ????? ?????? ?? ?? ?????? ???
    ???????? ? ???????????????.
  2. ???????? ???????? XML ???? catalog.xml, ?????????
    ?? ????????? XSD ?????. ???????? ?????
    catalog.xml ??? ??????????? ?? ????? ? ??
    ??????????? ?? ??? ? ??????? ?? VS.NET.
  3. ???????? ????????, ????? ? ??????? ?? DOM ???????
    ? ????????? XmlDocument ? XmlNode ??????? ??
    students.xml ??????? ?? ?????? ????????, ?????
    ???? ???? 2 ??????? ??????.

82
??????????
  1. ???????? ????????, ????? ? ??????? ?? DOM ???????
    ? ???? ?????????? ?? ???-??????? ?????? ? ???????
    ?????? ???????? ?????? ?? ??????, ????? ?? ??????
    ??? ????? catalog.xml. ?? ????? ????? ?????? ??
    ?? ???????? ???? ?? ???????? ?? ? ????????.
  2. ???????? ????????, ????? ? ??????? ?? DOM ???????
    ?????? ????? ????? ? ??????? ? ????????? ??????
    ?? ????? ??????? ?? ????? students.xml. ??????? ?
    ? ????????? ???????? "??? ?????" ? ????????????
    "??? ????????????" ? ?????? ?? ?? ?????? ???? ???
    ?? ?? ????? ? ??????? ?? ??????? ? ? ??????? ??
    ????????? ?????? ?? ????????.
  3. ???????? ????????, ????? ? ??????? ?? DOM ???????
    ??????? ?? ????? catalog.xml ?????? ??????, ?????
    ??????? ?????? ?? 20 ??.

83
??????????
  1. ? ??????? ???? ? ??????? ????????????? ????????
    ?????? ?? ???????? ????? ?????, ?????? ? ????????
    ?? ????? ?????. ???????? ????????, ????? ?
    ??????? ?? DOM ??????? ??????? ??? XML ????????,
    ????? ??????? ???? ????? ? ???????????? ???.
  2. ???????? ????????, ????? ? ??????? ?? ???????
    XmlReader ??????? ?????? ???????? ?? ?????? ??
    ????? catalog.xml.
  3. ???????? ????????, ????? ? ??????? ?? ???????
    XmlReader ??????? ? ????????? ?? ????? ??????? ??
    ????? students.xml ?????? ?? ??????? ?? ??????
    ?????????????, ??? ????? ????????? ? ???? ???????
    ??????? ?????.

84
??????????
  1. ? ??????? ???? ? ??????? ????????????? ????????
    ?????? ?? ???????? ????? ?????, ??????? ?
    ????????? ?? ????? ?????. ???????? ????????,
    ????? ? ??????? ?? ????? XmlWriter ??????? ???
    XML ????????, ????? ??????? ???? ????? ?
    ???????????? ???.
  2. ???????? ????????, ????? ? ??????? ?? ?????????
    XmlReader ? XmlWriter ??????? ????? catalog.xml ?
    ??????? ????? album.xml, ? ????? ??????? ??
    ???????? ????? ??????? ?? ?????? ?????? ? ???????
    ??????.
  3. ???????? ????????, ????? ????????? ????????
    ?????????? ?? ??????? ???? ? ??????? ? XML ????
    ??????? ?????????? ?????? ? ???????? ?
    ???????????????. ??????????? ???????? ltfilegt ?
    ltdirgt ? ????????? ????????. ?? ???????????? ??
    XML ????????? ??????????? ????? XmlWriter.

85
??????????
  1. ???????? ????????, ????? ???????? ?????
    students.xml ?? ??????????? ?? XSD ?????.
  2. ???????? ????????, ????? ? ??????? ?? DOM ??????
    ? ????????? XPath ?????? ?? ????? ??????? ??
    ????????? students.xml ??????? ???????? ?? ??????
    ? ??????? ?? ????? ? ????????? ???? ??????? ?
    ???????? ????????.
  3. ???????? ????????, ????? ? ??????? ?? ?????
    XPathNavigator ? ????????? XPath ?????? ???????
    ?? ????? catalog.xml ?????? ?? ?????? ??????,
    ???????? ????? 5 ??? ?????? ??????.
  4. ???????? ????????, ????? ? ??????? ?? XPath
    ?????? ????? DOM ??????? ?? ?????????
    students.xml ?????? ?? ????? ??????? ??????
    ??????, ????? ??? ? ???? ? ?????? ?????? (3) ?
    ??????? ???????? ?? ?? ??????? (6).

86
??????????
  1. ???? ?? ????? ?? ?????????? XSLT ?? ?????????????
    ?? XML ???????? ??? ?????????? ???? ???????
    ??????, ? ???? ??? ???? XML?
  2. ???????? ???????? XSL ??????, ????? ???????????
    ????? catalog.xml ? XHTML ????????, ???????? ??
    ??????????? ?? ?????????? Web-???????. ????????
    ????????, ????? ??????? ??????? ? ??????? ??
    ????? XslTransform.
  3. ???????? XSL ??????, ????? ?????? ???? ????
    ????????? students.xml ? ???????? ???? ????????
    ???? XML ????????, ???????? ???? ??????? ?
    ???????????? ?????? ?? ?????? ????????. ????????
    ????????, ????? ??????? ??????? ? ??????? ??
    ????? XslTransform.

87
?????????? ??????????
  • MSDN Library http//msdn.microsoft.com
  • ????? ????????, ?????? ? XML http//www.nakov.
    com/dotnet/2003/lectures/Working-with-XML.doc
  • MSDN Training, Introduction to XML and the
    Microsoft .NET Platform (MOC 2500A)
Write a Comment
User Comments (0)