Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Hungry Minds - Visual C# Blueprint.pdf
Скачиваний:
101
Добавлен:
12.02.2016
Размер:
9.71 Mб
Скачать

C#

SAVE XML TO A FILE

You will sometimes need to persist data as XML. In ADO.NET, the persistence mechanism for DataSets is XML. XML provides an excellent way to save and

retrieve data without a database server.

One of the fastest ways to write data is by using the

XMLTextWriter class that is part of the System.Xml namespace. This writer provides a fast, forward-only way of generating XML and helps you to build XML documents that conform to the W3C Extensible Markup Language (XML) 1.0 and the Namespaces in XML specifications. You can find the latest XML specification at www.w3c.org.

The XMLTextWriter is an implementation of the XMLWriter abstract class. You can write your own

implementation of this abstract class, but if the XMLTextWriter has what you need, you use this .NET Framework class. Typically, you use an XMLTextWriter if you need to quickly write XML to file, stream, or a TextWriter, and do not need to use the Document

Object Model (DOM).

The XMLTextWriter has formatting capabilities to assist in giving a file with nice indentions that are handy when reading the documents in a text viewer. When you construct your XML, you use one of several Write methods, depending on what part of the XML document you are constructing (element, attribute, or comment).

SAVE XML TO A FILE

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rename the namespace

 

Á Create the Main function.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

to XMLSamples.

 

Create an

 

 

 

 

 

 

 

 

 

Rename the class name

 

 

 

 

 

 

 

 

 

 

XmlTextWriter variable

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

to WriteXML.

and initialize the variable

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ˇ Save the file.

to null.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

° Set the XmlTextWriter variable equal to a new

XmlTextWriter, using the location of the XML file.

· Begin the XML document using the

WriteStartDocument method.

USING THE XML FRAMEWORK CLASS 13

You can use verbatim strings to handcraft XML and set the indention in your code. Remember that you will have to double up your quotes inside of the string.

TYPE THIS:

using System; using System.IO; using System.Xml; public class Sample

{public static void Main()

{ XmlDocument doc = new XmlDocument();

string sXML =

@""<?xml version=""1.0"" standalone=""no""?> <!—This file represents a list of favorite photos—> <photofavorites owner=""Frank Ryan"">

<photo cat=""vacation"" date=""2000""> <title>Maddie with Minnie</title>

</photo>

</photofavorites>"; // end of string doc.LoadXml(sXML); doc.Save("data.xml");

}}

RESULT:

XML document created in the internals of the class and echoed out to the console.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Begin an element using

 

 

¡ End the XML document

 

 

 

 

the WriteStartElement

by adding the Flush and

 

method.

Close methods.

 

Add an attribute to

Press F5 to save, build,

 

 

the element.

and run the console

 

 

± End the element using the

application.

 

 

 

 

 

 

 

 

WriteEndElement

 

 

 

 

 

 

method.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

£ Open the XML file that

 

The XML file has the

 

 

 

was created, which is located

 

elements and

 

in the bin\Debug directory.

 

attributes created.

247

C#

QUERY XML WITH XPATH

XML is great for portable data. If you want a quick way to query XML documents for pieces of data relevant to your application, XPath is a high-performance

mechanism to get this done. XPath is specified by W3C and is a general query language specification for extracting information from XML documents. XPath functionality has its own namespace in the .NET Framework. The System.Xml.XPath namespace has four classes that work together to provide efficient XML data searches.

The classes provided by System.Xml.XPath are:

XPathDocument, XPathExpression, XPathNavigator, and XPathNodeIterator. XPathDocument is used to cache your XML document in a high-performance oriented cache for XSLT processing. To query this cache, you will

need an XPath expression. This can be done with just a string that contains an XPath expression or you can use the XPathExpression class. If you want performance, you will use the XPathExpression class because it

compiles once and can be rerun without requiring subsequent compiles. The XPath expression is provided to a select method on the XPathNavigator class. The XPathNavigator object will return an XPathNodeIterator object from executing the Select method. After calling this method, the XPathNodeIterator returned represents the set

of selected nodes. You can use MoveNext on the XPathNodeIterator to walk the selected node set.

QUERY XML WITH XPATH

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Rename the namespace

 

Á Create the Main function.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

to XMLSamples.

Create a new

 

 

 

 

 

 

 

 

 

 

 

Rename the class name

 

 

 

 

 

 

 

 

 

 

XPathDocument using

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

to XMLwithXPath.

the location of the

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

ˇ Save the file.

XML document.

 

 

 

 

 

 

 

 

 

 

 

° Create a new

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

XPathNavigator using the

 

 

 

 

 

 

 

 

 

 

 

 

XPathDocument created.

· Create an

XPathNodeIterator variable that will contain the result of running an XPath query that returns all of the photo/title elements.

USING THE XML FRAMEWORK CLASS 13

You can use the recursive decent operator to search for an element at any depth. Make sure that the source XML document, photo_library.xml, is in the working directory of the EXE file.

TYPE THIS:

using System; using System.IO; using System.Xml; using System.Xml.XPath; namespace XMLSamples {

public class XMLwithXPath {

private const String sXMLDocument = "photo_library.xml"; public static void Main() {

Console.WriteLine ("XPath query results are:");

XPathDocument xpdPhotoLibrary = new XPathDocument(sXMLDocument); XPathNavigator xpnPhotoLibrary = xpdPhotoLibrary.CreateNavigator(); XPathNodeIterator xpniPhotoLibrary =

xpnPhotoLibrary.Select ("//photo/title"); while (xpniPhotoLibrary.MoveNext())

Console.WriteLine(xpniPhotoLibrary.Current.Name + " = " + xpniPhotoLibrary.Current.Value);

}}}

RESULT:

XPath query results are:

title = Fun at the Beach

title = Opening the gifts

Add a while loop to output the name and the value of the node to

the console.

Set a debug stop.

± Press F5 to save, build, and run the console application.

A message appears that shows the name and the value for the two elements that match the XPath query.

249

Соседние файлы в папке c#