Ways to process xml in c. How to open an XML document - features of working with XML files. Creating an XML Element with Children




Extensible Markup Language (XML). These are really just text files that use custom tags to describe the structure and other functions of the document.

What is XML

XML is a markup language created by the World Wide Web Consortium (W3C) to define a syntax for encoding documents that can be read by humans and machines. It does this through tags, which define the structure of the document, as well as how the document should be stored and transported.

The easiest way to compare it to another markup language you may be familiar with is hypertext markup(HTML) used to encode web pages. HTML uses a predefined set of markup characters (short codes) that describe the format of the content on a web page.

However, the difference with XML is that it is extensible. XML does not have a predefined markup language like HTML. Instead, XML allows users to create their own markup characters to describe content, creating an unlimited and self-defining set of characters.

Essentially, HTML is a language that focuses on presenting content, while XML is a dedicated data description language used to store data.

XML is often used as the basis for other document formats, of which there are hundreds. Here are a few you may want to know:

  • RSS and ATOM describe how applications should handle web feeds.
  • Microsoft .NET uses XML for its configuration files.
  • Microsoft Office uses XML as the basis for document structure.

So, if you have an XML file, it doesn't necessarily tell you what application it is for. However, you don't need to worry about this unless you are developing XML files.

How to open an XML file

There are several ways to open an XML file directly. You can open and edit them with any text editor, view them with any web browser, or use a website that allows you to view, edit, and even convert them to other formats.

Use a text editor when working with XML regularly

Since XML files are simply text files, they can be opened in any text editor. However, many text editors, such as Notepad, are simply not designed to display XML files with their correct structure. This can be useful for quickly familiarizing yourself with the contents of an XML file. But, there are more convenient tools for working with them.


Using a Web Browser to View an XML File

If you don't need to edit the XML file, but just want to view the content, your browser can handle this task. Moreover, your web browser is most likely already configured as a standard XML file viewer. So double clicking the XML file should open it in the browser.

If not, you can right-click the file to find options to open it with any other application. Simply select your web browser from the list of programs.

When the file opens, you should see well-structured data. It's not as pretty as the color look you get in Notepad++, but it's much better than notepad.

Using an online editor for XML files

If you want to edit a random XML file and don't want to download a new text editor, or you need convert xml file to another format There are several decent online XML editors available for free.

TutorialsPoint.com, XMLGrid.net and CodeBeautify.org - Allow you to view and edit XML files. Once editing is complete, you can download the modified XML file or even convert it to a different format.

For example, we use CodeBeautify.org. The page is divided into three sections. On the left is the XML file you are working with. In the middle you will find several functions. On the right you will see the results of some of the options you can choose from.

For example, in the image below, our complete XML file is on the left, and the results pane is showing a tree view because we clicked the Tree View button in the middle.

Use the Browse button to load an XML file from your computer or the Load URL button to retrieve XML from an online source

The Tree View button displays the data in a well-formatted tree structure in the results pane, with all tags highlighted in orange and attributes to the right of the tags.

Beatify displays your data in a neat, easy-to-read format.

The Minify button displays your data using as few spaces as possible. This function will place each piece of data on one line. This will come in handy when trying to make a file smaller, which will save some space.

Finally, you can use the XML to JSON button to convert the XML to JSON format, and the Export to CSV button to save the data as a comma separated values ​​file, or the Download button to download any changes you made to the new XML file .

You've probably heard of XML and know many reasons why it should be used in your organization. But what exactly is XML? This article explains what XML is and how it works.

In this article

Markups, markup and tags

To understand XML, it's useful to remember how you can tag data. Documents were created by people over many centuries, and throughout that time people made notes on them. For example, teachers often mark up student work to indicate the need to move paragraphs, make a sentence clearer, correct spelling errors, etc. Marking a document can help determine the structure, meaning, and appearance of information. If you've ever used revisions in Microsoft Office Word, then you're familiar with the computerized markup form.

In the world of information technology, the term "tagging" has evolved into the term "markup." Markup uses codes called tags (or sometimes tokens) to define the structure, visual appearance, and—in the case of XML—meaning of data.

The HTML text of this article is a good example of the use of computer markup. If you right-click this page in Microsoft Internet Explorer and select View HTML Code, you will see readable text and HTML tags like

AND

. In HTML and XML documents, tags are easy to recognize because they are enclosed in angle brackets. In the source text of this article, HTML tags perform many functions, such as defining the beginning and end of each paragraph (

...

) and the location of the drawings.

Features of XML

HTML and XML documents contain data enclosed in tags, but that's where the similarities between the two languages ​​end. In the HTML format, tags determine the design of the data - the location of headings, the beginning of a paragraph, etc. In the XML format, tags determine the structure and meaning of the data - what it is.

By describing the structure and meaning of data, it becomes possible to reuse it in several ways. For example, if you have a block of sales data in which each element is clearly defined, you can load only the necessary elements into the sales report and transfer the other data to the accounting database. In other words, you can use one system to generate and tag data in XML format, and then process that data in any other system, regardless of the client platform or operating system. This compatibility makes XML the basis of one of the most popular data exchange technologies.

Please note the following when working:

    HTML cannot be used instead of XML. However, XML data can be enclosed in HTML tags and displayed on web pages.

    HTML capabilities are limited to a predefined set of tags that are common to all users.

    XML rules allow you to create any tags required to describe the data and its structure. Let's say you need to store and share information about your pets. To do this, you can create the following XML code:

    Izzy Siamese 6 yes no Izz138bod Colin Wilcox

As you can see, the XML tags make it clear what data you are viewing. For example, it's clear that the data is about a cat, and you can easily determine its name, age, etc. Because you can create tags that define almost any data structure, XML is extensible.

But don't confuse the tags in this example with the tags in the HTML file. For example, if the above XML text is pasted into an HTML file and opened in a browser, the results will look like this:

Izzy Siamese 6 yes no Izz138bod Colin Wilcox

The web browser will ignore the XML tags and display only the data.

Well formed data

You've probably heard someone in IT talk about a "well-formed" XML file. A well-formed XML file must follow very strict rules. If it doesn't follow these rules, the XML doesn't work. For example, in the previous example, each opening tag has a corresponding closing tag, so this example follows one of the rules of a well-formed XML file. If you remove a tag from a file and try to open it in one of the Office programs, an error message will appear and it will be impossible to use such a file.

You don't need to know the rules for creating a well-formed XML file (although they are not difficult to understand), but remember that only well-formed XML data can be used in other applications and systems. If the XML file does not open, it is probably malformed.

XML is platform independent, which means that any program designed to use XML can read and process XML data, regardless of hardware or operating system. For example, if you apply the right XML tags, you can use a desktop program to open and process data received from the mainframe. And, regardless of who created the XML data, the data can be manipulated in a variety of Office applications. Due to its compatibility, XML has become one of the most popular technologies for exchanging data between databases and user computers.

In addition to well-formed tagged data, XML systems typically use two additional components: schemas and transforms. The following sections describe how they work.

Scheme

Don't be intimidated by the term "scheme". A schema is simply an XML file that contains rules for the contents of the XML data file. Schema files usually have the extension XSD, while XML data files use the XML extension.

Schemas allow programs to validate data. They form the structure of the data and ensure that it is understandable to the creator and other people. For example, if the user enters invalid data, such as text in a date field, the program may prompt the user to correct it. If the data in an XML file matches the rules in the schema, you can use any XML-enabled program to read, interpret, and process it. For example, as shown in the image below, Excel can validate data for compliance with the CAT scheme.

Circuits can be complex, and this article cannot explain how to create them. (Also, there are likely IT people in your organization who know how to do this.) However, it is useful to know what the diagrams look like. The following diagram defines the rules for a set of tags ... :

Don't worry if the example isn't clear. Just pay attention to the following:

    The string elements in the example schema are called declarations. If additional information about the animal was required, such as its color or special characteristics, the IT department would add appropriate declarations to the diagram. The XML system can be modified as business needs evolve.

    Declarations are a powerful means of manipulating data structure. For example, an ad means that tags such as And , must follow in the above order. Ads can also validate the types of user input. For example, the above circuit requires a positive integer input for the cat's age and Boolean values ​​(TRUE or FALSE) for the ALTERED and DECLAWED tags.

    If the data in an XML file follows the rules of the schema, then the data is said to be valid. The process of verifying that an XML data file conforms to schema rules is called (logically enough) validation. The big advantage of using schemas is that they can prevent data corruption. Schemas also make it easier to find corrupted data because XML file processing stops when a problem occurs.

Transformations

As discussed above, XML also allows for efficient use and reuse of data. The mechanism for reusing data is called an XSLT transformation (or simply transformation).

You (or your IT department) can also use transformations to exchange data between back-end systems, such as databases. Let's assume that database A stores sales data in a table that is useful to the sales department. Database B stores income and expense data in a table specifically designed for accounting. Database B can use a transformation to take data from Database A and put it into the appropriate tables.

The combination of the data file, schema, and transformation forms the basic XML system. The following figure shows the operation of such systems. The data file is checked against the schema rules and then passed in any suitable way for transformation. In this case, the transformation places the data in a table on a web page.

The following example shows a transformation that loads data into a table on a web page. The point of the example is not to explain how to create transforms, but to show one of the forms they can take.

Name Breed Age Altered Declawed License Owner

This example shows what the text of one type of transformation might look like, but remember that you may be limited to a clear description of what you need from the data, and that description can be done in your native language. For example, you might go to the IT department and say that they need to print sales data for specific regions for the last two years, and that the information should look like this and that. The department can then write (or modify) a transformation to fulfill your request.

Microsoft and a growing number of other companies are creating transforms for a variety of purposes, making XML even more convenient to use. In the future, it will likely be possible to download a conversion that suits your needs with little or no additional customization. This means that over time, using XML will become less and less expensive.

XML in the Microsoft Office system

Professional editions of Office provide enhanced XML support. Beginning with the 2007 Microsoft Office System, Microsoft Office uses XML-based file formats such as DOCX, XLSX, and PPTX. Because XML stores data in a text format rather than a proprietary binary format, clients can define their own schemas and use your data in a variety of ways without having to pay royalties. For more information about the new formats, see Open XML Formats and File Name Extensions. Below are other benefits.

This is all great, but what if you have XML data without a schema? This depends on which Office program you are using. For example, when you open an XML file without a schema in Excel, it assumes that the schema exists and allows you to load the data into an XML table. You can use XML lists and tables to sort, filter, and calculate data.

Enable XML features in Office

By default, the Developer tab is not visible. It must be added to the ribbon to use XML commands in Office.

The previous article described general concepts regarding XML. In this article we will learn how to perform basic actions related to changing, adding, searching in an XML file.

XML file that is used for the example.

Mazda 2007 BMW 2009

xml dom

At the moment, our file contains the following structure:

Relationship between nodes in XML DOM, basic moments:

1. Any node in the DOM tree has a parent ParentNode. In our example, garage is the parent of both car elements, and both car elements are, in turn, parents of model and year elements.

How to get the parent of the car xml element?

Console.WriteLine(elmRoot["car"].ParentNode.Name); //Result: garage

2. A parent can have children ChildNodes. For example, for the garage node the children are both car elements. The car elements also have children model and year.

ChildNodes, is a collection that stores all child xml elements; to access the desired element, you need to specify its index. (The index always starts from zero!)

For example: how to get the first child element?

ElmRoot.ChildNodes;

3. As in ordinary life, a child can be born first FirstChild, or last LastChild.

If we take the car element as an example, then

FirstChild is model LastChild is year

4. In turn, there are also connections between the child elements; they can be brothers or sisters, if we draw parallels with real life.

A child may have, for example, a Previous Sibling brother and a Next Sibling brother

Console.WriteLine(elmRoot.ChildNodes.FirstChild.NextSibling.Name); //Result: year Console.WriteLine(elmRoot.ChildNodes.LastChild.PreviousSibling.Name); //Result: model

If the element is not found, then an exception is thrown: NullReferenceException, so when working with xml, always use try catch blocks.

Console.WriteLine(elmRoot.ChildNodes. LastChild.NextSibling.Name); Console.WriteLine(elmRoot.ChildNodes. FirstChild.PreviousSibling.Name);

LastChild is NextSibling;
FirstChild is PreviousSibling;

Using the methods described above, you can easily move to the desired node and get any value you need.

How to get the value of an xml element?

The xml value of an element can be obtained using the InnerText property, for example:

Console.WriteLine(elmRoot["car"].FirstChild.InnerText); //Result: Mazda

Another way to get the same xml element value:

Console.WriteLine(elmRoot.FirstChild.FirstChild.InnerText); //Result: Mazda

The sequence of movements along the DOM tree:

Garage -> car -> model -> Mazda

We get the year:

ElmRoot["car"].LastChild.InnerText; //Result: 2007

Subsequence:

Garage -> car -> year -> 2007

Another example: 3 ways to get the same result.

Console.WriteLine(elmRoot.LastChild.FirstChild.InnerText); Console.WriteLine(elmRoot["car"].NextSibling.FirstChild.InnerText); Console.WriteLine(elmRoot.ChildNodes.Item(1).FirstChild.InnerText); //Result: BMW

If you need to get the year for an element with the value Mazda:

Console.WriteLine(elmRoot.FirstChild.LastChild.InnerText); //Result: 2007

For BMW (two ways, get the same result)

Console.WriteLine(elmRoot.ChildNodes.Item(1).ChildNodes.Item(1).InnerText); Console.WriteLine(elmRoot.ChildNodes.ChildNodes.InnerText); //Result: 2009

How to change xml element values?

Using property InnerText() You can both get and change the value of an xml element, for example, change the year.

//Set a new value elmRoot.FirstChild.LastChild.InnerText = "2010"; //Display the new value on the console screen Console.WriteLine(elmRoot.FirstChild.ChildNodes.Item(1).InnerText); //Result: 2010

At the same time, you need to remember that all changes occur with the virtual xml file; if you open the physical file, you will see that the year 2007 is still indicated in it.

In order for the changes to take effect, you need to use the Save method, for example:

ElmRoot.Save("xml file name or stream");

Now the information will be changed in the “physical” xml file.

How to get the number of child elements?

Console.WriteLine(elmRoot.FirstChild.ChildNodes.Count);

garage -> car contains 2 children: model and year

Console.WriteLine(elmRoot.FirstChild.FirstChild.ChildNodes.Count);

garage -> car -> model contains 1 child xml element.

Accessing Child Elements

by index

ElmRoot.ChildNodes.Name; elmRoot.ChildNodes.Name; //Result: car

Using a loop

Foreach (XmlNode nod in elmRoot.ChildNodes) ( Console.WriteLine(nod.Name); ) //Result: car, car

How to get xml element name?

elmRoot.Name; //Result: garage

Creating a new XML element

Let's create a new element in our XML document so that it differs from the other two (car), let's call it bus (bus).

When creating a new element, we will use the recommendation from the msdn website and instead of the standard new XmlElement we will use the CreateElement method.

XmlElement elm = xmlDoc.CreateElement("bus");

Creating and adding a new xml element

Let's create a new xml element named "BUS".

XmlElement elmRoot = xmlDoc.DocumentElement; Console.WriteLine(elmRoot.ChildNodes.Count); //car, car XmlElement elmNew = xmlDoc.CreateElement("bus"); elmRoot.AppendChild(elmNew); Console.WriteLine(elmRoot.ChildNodes.Count); //3 car, car, bus xmlDoc.Save("xml file name");

Explanation:

1. First we get a root element to which we will attach new elements.

2. As a check, we will display the current number of child elements of the garage element: 2 (car and car)

3. Create a new BUS element

4. Using the method AppendChild adding a new element to the tree

5. Let’s use the check again and display the current number of elements for the garage element, now there are 3 of them: car, car, bus.

6. For changes to affect the physical file, save

In the XML file itself, the new element will look like this:

How to add a new xml element?

Task: create a new XML element and add some text content to it, for example the year of manufacture.

String strFilename = @"C:\lessons\Auto.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(strFilename); XmlElement elmRoot = xmlDoc.DocumentElement; XmlElement elmNew = xmlDoc.CreateElement("bus"); XmlText new_txt = xmlDoc.CreateTextNode("2006"); elmRoot.AppendChild(elmNew); elmRoot.LastChild.AppendChild(new_txt); Console.WriteLine(elmRoot.ChildNodes.Name); //bus Console.WriteLine(elmRoot.ChildNodes.LastChild.InnerText); //2006 Console.Read();

In XML file:

2006

For clarity

Now let’s create a “bus” node with the same architecture as car, that is, add nodes: model, year and some text content.

Creating an XML Element with Children

string strFilename = @"C:\lessons\Auto.xml"; //create a new xml document in memory XmlDocument xmlDoc = new XmlDocument(); //load the xml file into memory xmlDoc.Load(strFilename); //Get the root element XmlElement elmRoot = xmlDoc.DocumentElement; //Create 3 elements: bus, model, year XmlElement elmBUS = xmlDoc.CreateElement("bus"); XmlElement elmModel = xmlDoc.CreateElement("model"); XmlElement elmYear = xmlDoc.CreateElement("year"); //Set values ​​for elements: model, year XmlText year_txt = xmlDoc.CreateTextNode("2006"); //XmlText mod_txt = xmlDoc.CreateTextNode("liaz"); add otherwise //Add two child elements to the bus element: model and year elmBUS.AppendChild(elmModel); elmBUS.AppendChild(elmYear); //Add values ​​to the model and year nodes elmModel.InnerText = "liaz"; elmYear.AppendChild(year_txt); //Add a new xml element bus to the tree elmRoot.AppendChild(elmBUS); //Check if everything is added as it should Console.WriteLine(elmRoot.ChildNodes.FirstChild.InnerText); Console.WriteLine(elmRoot.LastChild.LastChild.InnerText); //If everything is in order, then use the Save method xmlDoc.Save("xml file name");

Result:

liaz 2006

How can I shorten this code? For example, as follows:

String PathXmlFile = @"C:\lessons\Auto.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(PathXmlFile); XmlElement elmRoot = xmlDoc.DocumentElement; XmlElement elmBUS = xmlDoc.CreateElement("bus"); XmlElement elmModel = xmlDoc.CreateElement("model"); XmlElement elmYear = xmlDoc.CreateElement("year"); //Add values ​​to the model and year nodes elmModel.InnerText = "liaz"; elmYear.InnerText = "2006"; elmBUS.AppendChild(elmModel); elmBUS.AppendChild(elmYear); elmRoot.AppendChild(elmBUS); //If everything is correct, then call the Save method xmlDoc.Save("xml file name");

Let's shorten the code a little more, for this we will use the InnerXml property:

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(PathXmlFile); XmlElement elmXML = xmlDoc.CreateElement("bus"); string txt = " liaz" + "2006"; //InnerXml! elmXML.InnerXml = txt; //xmlDoc.DocumentElement (will be equal to garage) - this is another way to access the root element, the same as XmlElement elmRoot = xmlDoc.DocumentElement; xmlDoc.DocumentElement.AppendChild( elmXML); xmlDoc.Save(PathXmlFile);

Result

Get a list of elements using GetElementByTagName

GetElementByTagName returns XmlNodeList, which contains all descendant elements belonging to the specified element, for example, we need to get all the car models that are stored in the garage:

XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(strFilename); XmlNodeList modelName = xmlDoc.GetElementsByTagName("model"); foreach (XmlNode node in modelName) ( Console.WriteLine(node.InnerText); ) //Result: mazda, bmw, liaz

Access using index:

String PathXmlFile = @"C:\lessons\Auto.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(PathXmlFile); XmlNodeList modelName = xmlDoc.GetElementsByTagName("model"); Console.WriteLine(modelName.InnerText); //Result: liaz

How can I change the text content of a newly created "bus" element using the GetElementByTagName method?

String PathXmlFile = @"C:\lessons\Auto.xml"; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(PathXmlFile); XmlNodeList modelName = xmlDoc.GetElementsByTagName("model"); Console.WriteLine(modelName.InnerText); //Received the value: liaz

Or you can change the name liaz to Ikarus

Console.WriteLine(modelName.InnerText = "Ikarus");

Quite a long time has passed since I promised to talk about working with data in XML format when developing .NET applications. Promises must be kept. Please note that this article is not aimed at professional .NET developers, but at those who do not yet have significant experience in creating .NET applications.


Why should we work with XML?

If you don’t yet have a very good idea of ​​what XML is, then I recommend reading the article “XML is serious and for a long time” in “KV” No. for 2007. To save space for more important things, the XML format itself will not be analyzed here.

Before you start getting acquainted with the program code, you need to answer one fundamental question: why even include XML support in your application? I think many people have asked themselves this question regarding many technologies, and not all of them were included in applications after the answer. However, with XML the situation is somewhat different, and there are real reasons to use this format in many situations.

The main advantage of XML is that, being a text format by nature, it nevertheless perfectly allows storing and transmitting any data. Since this format is text, the problem of its cross-platform is solved by itself, and the problem of transmitting XML data is just as easily solved (as is, for example, done in SOAP). In addition, you can easily change the data transfer or storage format by adding new attributes and without worrying too much about compatibility with previous versions of the format, since applications using the old version will be able to read what they need without paying attention to new tags or attributes . Many applications use XML-based file formats, many data transfer protocols are also based on XML, and the lists of both continue to grow.

Of course, on the other hand, XML is not very economical, because, as one of the visitors to the Computer News forum once correctly noted, XML documents often consist of 10% data and 90% tags. However, this largely depends on what tags you choose. You can write st. Melnikaite, 2, Can i . Although, to be honest, it seems to me that with the current hard drives and thick channels there is no point in being particularly compressed.

So in the right hands, XML is a powerful and convenient thing, and due to the ubiquity of this format, you can’t escape it at all. So let's move on to writing program code.

For programming we will use the main language of the .NET platform - C#. In order for as many readers as possible to practice with the given program code themselves, I will use the first version of C# and the .NET Framework.


Data recording

First, let's talk about writing data, because, you know, in order to read something from somewhere, you must first write something somewhere. And since you and I started programming, it’s not appropriate for us to create XML data manually. So let's first get started with writing the data into XML.

First, create a new project in Visual Studio, #Develop or C# Builder, and add System.Xml to the list of imported namespaces.

A special class, XmlTextWriter, is responsible for writing XML data in the .NET Framework, which allows you to write XML data to an arbitrary stream. That is, we, generally speaking, can use it to write it to a file, a database, and send to someone via the Internet, but now we will write everything to a file. You can redirect the output by changing the object constructor (i.e., passing not the file name and its encoding during initialization, but an object that is a data stream). I think I’ve already gotten a little ahead of myself. Let’s first get acquainted with the code responsible for writing data to our XML file.

String FileName = "c:\\demo.xml"; XmlTextWriter xml = new XmlTextWriter(FileName, System.Text.Encoding.Unicode); xml.Formatting = Formatting.Indented; xml.WriteStartDocument(); xml.WriteStartElement("rootelement"); for (int i = 0; i< 10; i++) { xml.WriteStartElement("subelement"); xml.WriteAttributeString("attrib1", "value1"); xml.WriteAttributeString("attrib2", i.ToString()); for (int j = 0; j < 10; j++){ xml.WriteStartElement("subsubelement"); xml.WriteAttributeString("attr", j.ToString()); xml.WriteEndElement(); } xml.WriteEndElement(); } xml.WriteEndElement(); xml.WriteEndDocument(); xml.Close();

The first line, I think, is quite clear - it’s simply recording the name of the file into which we will save the data. Next, we create an object of the XmlTextWriter type (it is called, as you can see, xml), and it is with it that we will perform all further operations. Please note that when constructing an object, we also specify the encoding in which the XML will be written: in our example, this is Unicode. The next line, generally speaking, is not required, but it will make our XML document, as they say, human readable, that is, it will add the necessary indents and break it into lines. Without this, the entire document would be written on one line, which, although it saves space, makes it practically unsuitable for manual editing.

Document writing begins by calling the WriteStartDocument() method of our xml object. The line following it adds the root element “rootelement” to our XML document (let me remind you that for XML documents the root element must be present in a single copy). Next, in a cycle, we add ten more elements that do not carry any semantic load to our XML document, for each of which we set two attributes and ten more subelements. Please note that we can add a number to a string without explicitly converting the data, but if the number must completely form a string, then it must be converted explicitly using the ToString() method. Also note that we must explicitly close each of the elements of our XML document, and then the entire document.

Now that our XML document has been successfully written, let's see how we can read data from it.


Reading data

Add a listBox component to the form of your application (unless, of course, it is a console application) so that you can monitor the result of reading the XML file. Well, if your program is a console program, then you can easily redirect the output to the console.

As usual, let's first get acquainted with the program code, and then we will look at what exactly this code does.

XmlTextReader xml = new XmlTextReader(FileName); xml.WhitespaceHandling = WhitespaceHandling.None; int i = 0; while (xml.Read())( if ((xml.NodeType == XmlNodeType.Element) & (xml.Name == "subelement")) ( listBox1.Items.Add("subelement " + i + " found") ; i++; listBox1.Items.Add(" " + xml.GetAttribute("attrib1")); listBox1.Items.Add(" " + xml.GetAttribute("attrib2")); xml.Name == "subsubelement"))( listBox1.Items.Add(" " + xml.GetAttribute("attr")); ) ) ) xml.Close();

For reading, as you may have noticed, we use another class, namely XmlTextReader. It is in the same namespace as the class we used to write the data. In the first line, we create an instance of XmlTextReader, named xml (here we assume that the FileName variable was already defined by us earlier). To skip empty lines if they somehow inexplicably appear in our just created XML file, we use the following line in the given code fragment. The i variable is used to count the number of "subelement" elements found in the XML file from which the data is read.

Next comes the cycle of directly reading data from the file. The Read() method reads the next element of the XML document, and after reading it, we check what exactly we read. If it is indeed a "subelement" element, then we add information about the element read to listBox1, increment a variable containing the number of elements read, and then read the element's attributes. After reading the attributes, we organize a separate loop to read the subelements (note that we do not need a separate XmlTextReader for this) and the attributes of these subelements. As before, we enter all the information read into listBox1 to control the correctness of reading.

When reading XML files, in the same way as when writing them, when constructing an XmlTextReader, you can specify as a parameter the stream from which to read, and then you can read not only from files, but also from other sources , examples of which I have already given above. One useful feature of the XmlTextReader class should be noted: when reading, it does not load the entire XML document being read into memory, so it is convenient to parse large XML documents (for example, XML databases).


Behind the scenes

In general, the example we just discussed is too simple for real projects. Nowadays, when reading XML documents, they are usually validated using DTD, XML Schema or Relax NG. Validation is a check of compliance of the XML document markup with some standard described in an external file. Validation is needed so that document verification is not hard-wired into the program algorithm, but can be changed arbitrarily when the data format changes without updating the program code that reads or writes data. Unfortunately, now we won’t have time to sort out the validation, because, as you yourself understand, the volume of a newspaper article has certain limitations.

Another interesting and useful practical point regarding working with XML data is XSL data transformation. This transformation is applied to data when it is displayed on HTML pages and, in fact, is simply applying a specific web page template to an XML file containing some data. Since the lion's share of the current use of XML data is somehow on the World Wide Web, it would be very, very good to consider XSL transformations.

So, I think, this article will have a continuation - but, of course, only if you yourself (that is, the readers of Computer News) ask about it on the forum or in a letter to my email address. For now, that’s probably all about using XML in .NET applications. I hope this information is useful to you.