Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Microsoft CSharp Programming For The Absolute Beginner (2002) [eng]-1.pdf
Скачиваний:
46
Добавлен:
16.08.2013
Размер:
15.71 Mб
Скачать

Table 10.2 demonstrates a few key properties and methods of the XmlDocument class.

Note that the CreateElement() and CreateAttribute() methods create the specified structures but do not specifically add them to the document.

Use the XmlNode.AppendNode(), XmlNode.InsertBefore(), or XmlNode.InsertAfter() method to add a node and the XmlElement.SetAttributeNode() method to set an attribute to a node.

Table 10.2: Selected Members of the XmlDocument Class

Member

Type

Description

Example

Example

 

 

 

 

Description

DocumentElement

Property

Sets or gets the

MessageBox.Show

Displays the root

 

 

root XML

(doc.DocumentElement);

element for an

 

 

element for the

 

XmlDocument

 

 

document

 

named doc

CreateAttribute

Method

Creates a new

doc.CreateAttribute

Creates a new

 

 

attribute for an

(name) ("type");

attribute but

 

 

element with

 

does not add it

 

 

the specified

 

 

 

 

name

 

 

CreateElement

Method

Creates a new

doc.CreateElement

Creates a new

(name)

 

element

("question");

element but

 

 

 

 

does not add it

Load(filename)

Method

Loads an XML

doc.Load

Loads the

 

 

document from

("myStuff.xml");

specified file into

 

 

the given file

 

the document

 

 

name

 

 

LoadXml

Method

Interprets a

doc.LoadXml

Loads the text

(xmlString)

 

string value as

("<simpleXml>this is

value as a

 

 

XML

simple</simpleXml>");

simple XML

 

 

 

 

document

Save

Method

Saves the

doc.Save

Saves thecurrent

(filename)

 

document to

("myStuff.xml");

document to a

 

 

the specified

 

file named

 

 

file

 

myStuff.xml

Reading an Existing XML Document

The only way to make sense of all this information is to build a program that puts it to use. The XML Viewer program will help you see how exactly C# reads an XML document and how to get around in it. The XML Viewer is more than a programming exercise. It is a tool you can use to analyze and explore the structure of any XML document. After you see how it describes the data in an XML document, you will be ready to get inside the engine and see how the program does its work. Figure 10.7 illustrates the XML Viewer as it first loads the quiz document.

287

Figure 10.7: The starting node is named xml, but it isn’t very interesting.

The root node is the xml tag itself. It usually provides some background information, but it doesn’t have any children and doesn’t contain a lot of other data. To get to the interesting parts of the file, you click the Next Sibling button (see Figure 10.8).

Figure 10.8: The test node has a lot more information. (Almost too much!)

288

The test node is much more interesting. All the interesting data in the quiz file was somehow related to the test node, so it isn’t surprising that the test node shows much more information. All the screen elements in the XML Viewer program describe properties of the current node. The Parent button refers to the test node’s parent node, which is the document itself. I disabled the Parent button in this particular case because the document is the top layer. The node field refers to the Name property of the current node. In this case, the name of the current node is test. The value label refers to the Value property of the current node. You might be surprised that the test node has no value at all. That is because all the information of the test is actually stored in its children. As you will see, the InnerText property often turns out to be more useful than the Value property.

The InnerXml property returns all the XML associated with the current node. When test is the current node, all the XML between the <test> and </test> tags in the original XML show up as the InnerXml property. The InnerText property simply strips out the XML tags from the InnerXml property and returns the resulting text. Notice that the Next Sibling button is disabled because the program has detected that the test element does not have a next sibling.

A node can have several child nodes, as you can see from the test node. In this particular file, the test has three problems, and you can see a list box with three problem nodes. To view one of the child nodes, double−click it. I chose the first problem, which results in Figure 10.9.

Figure 10.9: I have clicked the first problem, and the viewer is now looking at the problem node.

All the screen elements have changed to reflect that the problem node has the focus. The test node is now listed as the parent node, and you can click the button to return to the parent node. (That’s why I made the parent node a button instead of a label.) The problem node still doesn’t have a value because it is mainly a container for other nodes. You can see that the InnerXml and InnerText properties have narrowed their scope considerably. Also, the list of ChildNodes shows the question, possible answers, and correct answer. The Next Sibling button is activated because this question has siblings. Click the Next Sibling button a couple times until it becomes disabled. It will do so at the last question, which gives you a screen like Figure 10.10.

289

Figure 10.10: Now the viewer is pointed at the last child of the test node.

It’s time to investigate the children of the problem node. Click the first child (the question), and you will see something like Figure 10.11.

Figure 10.11: You might be surprised that there still isn’t anything useful in the value field.

It would seem as though the question is the lowest level of the XML document, but it isn’t. The InnerText and InnerXml of the question (and all its siblings) are useful information, but you will see

290

that the question has one child, marked as #text. Not until you get to the #text node do you finally have actual data in the value field. The XML Viewer program is particularly interesting because it can be used to analyze any legal XML document. This points out one of XML’s advantages. The format of the data describes the data’s meaning, so a program can work with data in a new format with some hope of deciphering it.

Figure 10.12 illustrates how the .NET framework sees the quiz document.

291

Figure 10.12: This diagram more accurately reflects how .NET sees an XML document.

Trick

292

Соседние файлы в предмете Программирование на C++