Professional IronPython


Using the .NET XML Functionality



Download 20,35 Mb.
Pdf ko'rish
bet285/453
Sana01.07.2022
Hajmi20,35 Mb.
#723045
1   ...   281   282   283   284   285   286   287   288   ...   453
Using the .NET XML Functionality
 

 
275
The code begins by importing 
clr
, which the application uses to add the required reference to 
System.Xml
using the 
clr.AddReference()
method. The code then imports the 
System.Xml
classes.
The example relies on two functions to keep the code simple: 
CreateDocument()
, which creates and 
saves the document to disk, and 
DisplayDocument()
, which reads the document from disk and dis-
plays the content on screen. The example calls each of these functions in turn.
The 
CreateDocument()
function begins by creating an 
XmlDocument
object, 
Doc
. As with any .NET 
application, 
Doc
doesn’t contain anything when you create it. The first task is to add the XML dec-
larations so that the result is a well-formed XML document using 
Doc.CreateXmlDeclaration()

Calling 
Doc.AppendChild()
adds the declaration to the document.
Now it’s time to create some content. All XML documents have a root node, which is 
Root
for this 
example. The code creates 
Root
using 
Doc.CreateNode()
with an 
XmlNodeType.Element
type and 
‘root‘
for a name. The example doesn’t work with XML namespaces, so the third argument is set 
to 
None
.
The most efficient way to create an XML document from scratch is to add all the child nodes to 
Root
before you add 
Root
to the document. The code creates 
MsgNode
using the same technique as 
for 
Root
. It adds content to 
MsgNode
using the 
MsgNode.InnerXml
property and then adds the node 
to 
Root
using 
Root.AppendChild()
. The example provides two 
‘Message‘
nodes.
At this point, the code adds
Root
to the document using 
Doc 
.AppendChild()
. It then saves the 
document to disk using 
Doc.Save()

Figure 13-1 shows the typical output 
from this example when viewed in 
Notepad (you can use any text editor 
to view the output because the 
Doc 
.Save()
method includes spaces and 
line feeds).
The 
DisplayDocument()
function begins by creating a document, 
XMLDoc
, using the 
XmlDocument
class constructor. It then loads the previously created XML document using 
XMLDoc.Load()
. At this 
point, 
XMLDoc
contains everything the code created earlier and you can easily explore it using the 
IronPython console.
If you’ve worked with XML documents using C# or Visual Basic.NET, you know that these languages 
sometimes make it hard to get to the data you really want. IronPython makes things very easy. All you 
need is a 
for
loop, as shown in the code. Simple 
if
statements make it easy to locate nodes of a par-
ticular type, 
XmlElement
in this case.
By the time the code reaches the second 
for
loop, it’s working with the 
‘Message‘
elements. The 
code simply prints the 
MsgNodes.InnerXml
property value to the screen, as shown in Figure 13-2. 
By now you can see that IronPython makes it incredibly simple to work with XML documents using 
the .NET Framework approach.
FIgURE 13-1: 
The XML document output looks much as you
might expect.
548592c13.indd 275
2/24/10 12:48:57 PM
www.finebook.ir



Download 20,35 Mb.

Do'stlaringiz bilan baham:
1   ...   281   282   283   284   285   286   287   288   ...   453




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish