Familiarity with modern tools for working with XML. Reading data from a file
We present to your attention new course from the team The Codeby- "Penetration testing of Web Applications from scratch." General theory, preparation of the working environment, passive fuzzing and fingerprinting, Active fuzzing, Vulnerabilities, Post-exploitation, Tools, Social Engeneering and much more.
XML DOM 2
The previous article described general concepts concerning 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.
xml dom
IN at the moment, our file contains the following structure:
Relationship between nodes in XML DOM, main points:
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 for xml element and car?
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 refer to the required 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
It must be remembered that all changes occur with the virtual xml file Oh, 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 name file 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: garageCreating a new XML element
Let's create new element in our XML document, to make it different from the other two (car), let's call it 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:
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 one 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 to the bus element child elements: 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:
How can you 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, to do this we'll use the InnerXml property:
XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(PathXmlFile); XmlElement elmXML = xmlDoc.CreateElement("bus"); string txt = "
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");
Working with data in XML format in ASP.NET 2.0 applications, the XmlDataDocument object and the XML control
This module focuses on how you can work with XML data from ASP.NET applications.
XML stands for Extensible Markup Language, although XML itself is not a language. XML is a set of rules used to create your own markup languages. For example, imagine that we have the following data about an employee:
This data, of course, can be presented in any way you like: in the form of a table in relational database data, in the form Excel tables or HTML, as a table in Word document or in the form text document ASCII, as a *.csv file, etc. If we present them in the format of the XML-compatible markup language ABML (Address Book Markup Language) that we invented, they will look like this:
ADDRESS >
ADDRESS _ BOOK >
A little about how to decipher this code. The first two lines are a prologue (using it is, in principle, optional, but highly recommended). Line
xml version ="1.0"?>
is called an XML Declaration and indicates that the file conforms to the XML 1.0 specification, adopted as a recommendation by the World Wide Web Consortium on February 10, 1998. Line
is called a Document Type Definition and means that the structure of the language to which this document corresponds is described in the abml.dtd file (you can also use internal DTDs when the language description is located directly in the document). Nowadays, rather than DTDs, XML Schema is more often used to describe the structure of XML-compatible languages - they are easier to access and provide more capabilities, in particular, when describing various data types. The same line using XML Schema could look like this:
depending on where the Schema itself - the description of this language - is located in the abml.xml file or on the Web server (corporate schema storage from Microsoft - BizTalk Server).
An example XML Schema for our language might look like this:
XML is a formalized set of rules for “marking up” a document—that is, highlighting its logical structure. What's inside any XML-compliant document can be broken down into two categories: markup and the content itself. All markup information must begin with either an ampersand character (&) or a left angle bracket character (<). В XML существует шесть типов информации разметки: элементы, атрибуты, комментарии, инструкции обработки, ссылки на сущности и разделы CDATA.
· Elements(elements) is the most common type of markup information. The element identifies a logical component of a document. A typical document consists of opening and closing tags, which may surround content, another element, or both. Tags with element names are enclosed in angle brackets. Here is an example element:
· Attributes(attributes) consist of an attribute name/attribute value pair and are applied to elements. Attributes should be placed after the element name in the opening tag. For example, the attributes are width and height:
· Comments(comments) is any text that will be ignored by the XML processor. Example:
· Processing instructions(processing instructions) are used to pass information to the application processing the XML document. The processing instruction syntax looks like this:
· Entity references Entity references are used to place reserved characters or reserved words into a document. For example, we need to insert a left angle bracket (<), которая является зарезервированным символом XML. Просто так вставить в текст документа мы ее не сможем: приложение, работающее с документом, решит, что она относится к разметке. Поэтому нам необходимо использовать сочетание символов <. lt означает less than(less than), and the ampersand (&) and semicolon (;) highlight an entity reference.
· CDATA section(CDATA section) is a piece of text that is not processed like the rest of the XML document, but is passed directly to the application. This tool can be useful, for example, when passing some code to an application.
XML syntax principles:
· XML documents are composed of Unicode characters (Unicode is a 16-bit character set that allows documents to be displayed in any language).
· XML is case sensitive. Tags And there are different tags in it.
· Whitespace is invisible characters such as space (ASCII 32), tab (ASCII 9), carriage returns (ASCII 13), and line feeds (ASCII 10). White space is ignored within tags, but is preserved in character data (that is, between the opening and closing tags). White space in character data is passed to the processing application.
· Many XML components must have names (the most obvious example being elements and attributes). The XML naming rules are as follows: The XML name must begin with a letter or underscore, followed by any number of letters, numbers, hyphens, underscores, or periods, for example:
My_Unique_Tag_Identifier-123 2_This_name_is_incorrect
· The XML component name cannot begin with xml characters (either uppercase or lowercase). Such names are reserved by the specification creators for official purposes.
· Character values must be enclosed in single or double quotes.
· In XML, the nesting order of tags must be strictly observed.
· Any opening tag in XML must have a corresponding closing tag.
· An empty tag in XML is written as an opening tag, preceded by a right angle bracket with a forward slash (/).
· There can only be one root element in an XML document.
What are the advantages of storing data in XML over traditional binary formats? Why are most major software manufacturers currently either completely switching to working with data in an XML-compatible format (for example, Microsoft Office 2003), or planning to switch in the near future? The main reason is that XML data is very easy to transfer between a wide variety of applications and very easy to transform. Additional points related to the benefits of XML:
- Independent data format - data in XML format can be opened in any XML-compatible (more precisely, compatible with a specific schema) application. Example: at any enterprise, documents are stored in a variety of formats - formats of different versions of Word, text, HTML, PDF, etc. Because of this, a lot of problems arise, which can be radically solved with the help of XML.
- The general principle is one data source (XML document), many views. This can be clearly demonstrated using the example of a Web site that needs to be accessed from different browsers and via WAP.
- Much easier data transfer “through” applications. Examples are the passage of documents through a supply chain, or the passage of data between dissimilar software products in the same enterprise (which is necessary very often).
- Improved data search capabilities. Firstly, there is no need to access documents of different binary formats, and secondly, the hierarchical structure of XML documents makes searching easier.
- Easier application development - There is no need to implement support for a large number of different binary data formats in applications.
- Data in text format (XML - Unicode standard) is easier than binary to store on various platforms and safer (from the point of view of the absence of malicious binary code) to be transmitted over networks. A whole direction in application development is XML Web services.
Well-formed XML is XML code that conforms to the syntax requirements of that language (for example, each opening tag has a corresponding closing tag). Valid XML is valid in terms of the logical structure of the language (for example, elements are nested correctly) as defined in the DTD or XML Schema.
A little bit of XML terminology that will be used in this course:
· XSD - XML Schema Definition, a description of the structure of an XML document commonly used in VS .NET. Usually it is located in files with the *.xsd extension. Special tags are used inside the schema
xmlns:od="urn:schemas-microsoft-com:officedata">
To define a Last Name string element within a schema that can occur 0 or more times in a document, you can, for example, use the following tag:
type="string">
· to describe transformations of XML-compatible documents, documents in a special programming language XSLT (eXtensible Stylesheet Language Transform) are used. The language itself is, of course, also XML-compatible. XSLT uses three types of documents:
o source document. This XML document is "input" for transformation. In our case, this could be a document like this:
xml-stylesheet type="text/xsl" href=" Employees1.xsl"?>
o XSLT style sheet document - an XML-compatible document that describes the rules for carrying out transformations. In our case, an example of this document could be like this:
xmlns:xsl=" http://www.w3.org/1999/XSL/Transform"
version=" 1.0 ">
o document - the result of transformations. For example, when applying our transformation, the employee's name will be marked in red and his position will be marked in blue.
· XPath is a special language that can be used to navigate through a tree of XML elements. When using the XPath object model, an XML document is represented as a tree of nodes. The information is contained in the properties of these nodes.
· DOM (Document Object Model) - representation of an XML document tree in RAM. DOM allows you to navigate and edit an XML document. Standard DOM properties, methods, and events are defined in a document adopted by the W3C. In ASP.NET, you can use the DOM to create an XML document and send it to the user's browser. Another option is for a client script to create an XML document on the client using the DOM and send it to the Web server.
· XQuery is a specialized language for querying information stored in XML documents. XQuery is largely based on XPath.
It must be said that XML is a standard format for working with data in ADO.NET. About the XML format and how it can be used with DataSet - below.
The possibilities of using XML when working with DataSet are as follows:
· DataSets can serialize data in XML format. The schema of the DataSet (including tables, columns, data types and constraints) is defined in the XML Schema (.xsd file).
· data exchange from the DataSet with remote clients is required to be carried out in XML format;
· XML can be used to synchronize and transform data into a DataSet.
A little more about the interaction between XML and DataSet:
· you can not only create an XML Schema based on a DataSet (as mentioned above, this is done using the WriteXmlSchema method), but also vice versa - generate a DataSet based on the information from their XML Schema (for this, use the ReadXmlSchema method). It is possible to generate a DataSet even without a schema - simply based on an XML document. The InferXmlSchema method is intended for this purpose.
· a method is provided for the DataSet object ReadXML, which allows you to read an XML text document (XML text data stream) into a DataSet. Another method WriteXML, allows you to save the contents of a DataSet in an XML-compatible format. This feature makes it very easy to organize data exchange between different applications and platforms.
· You can create an XML representation (XmlDataDocument object) based on information from the DataSet. It turns out that you can work with information in a DataSet in two ways: conventional relational (with the DataSet itself) and XML methods. Both views are automatically synchronized (when changes are made through either view).
· You can apply XSLT transformations to data that is stored in a DataSet.
Now let's talk about how all this looks in practice.
Dim ds As New DataSet()
ds.ReadXml(Server.MapPath("filename.xml"))
The MapPath method for a special Server object allows you to convert the virtual path of a file in a Web application to a physical path.
Whether the DataSet structure is generated automatically from the XML file or remains the same depends on whether it has already been generated in that DataSet and whether the ReadXml XmlReadMode optional parameter was specified.
Dim ds As New DataSet()
Dim da As New SqlDataAdapter(_
"select * from Authors", conn)
da.Fill(ds)
ds.WriteXml(Server.MapPath("filename.xml"))
There are two more methods that allow you to get data in XML format from a DataSet and put it in a string variable. These are the GetXml and GetXmlSchema methods. An example might look like this:
DimstrXmlDSAsString =ds.GetXml()
A DataSet often contains DataTable objects connected to each other by DataRelations (that is, tables with a Primary and Foreign key). When exporting to XML, information from the parent table can be supplemented with information from the child table. Records from the subordinate table will appear as nested elements for records from the main table. To implement this feature, you need to set the Nested property to True for the DataRelation object in the DataSet (the default is False).
Let's say we just export the data without using this property:
Dim ds As New DataSet()
'fill the DataSet
...
Dim parentCol As DataColumn = _
ds.Tables("Publishers").Columns("pub_id")
Dim childCol As DataColumn = _
ds.Tables("Titles").Columns("pub_id")
ds.Relations.Add(dr)
ds.WriteXml(Server.MapPath("PubTitlesNotNested.xml"), _
XmlWriteMode.IgnoreSchema)
The XML code looks like this:
and now we use it to set the Nested property for the DataRelation object to True:
Dim dr As New DataRelation _
("TitlePublishers", parentCol, childCol)
dr.Nested = True
ds.Relations.Add(dr)
ds.WriteXML(Server.MapPath("PubTitlesNested.xml"), _
XmlWriteMode.IgnoreSchema)
The XML code turns out completely different. Each element of the Pub type contains Titles elements released by this publisher:
XmlDataDocument is an in-memory XML representation of data in a DataSet. An XmlDataDocument is inextricably linked to a DataSet. Any changes made to the XmlDataDocument are immediately reflected in the DataSet and vice versa. Below we will talk about techniques for working with XmlDataDocument.
DataSet is a relational representation of data, while XmlDataDocument is hierarchical. Using XmlDataDocument is very convenient, since working with data in XML format only through a DataSet can be difficult. For example, if you load data from an XML file into a DataSet and then unload it back, it may well turn out that the file will be unrecognizable: formatting will be lost, quite possibly the order of elements, perhaps elements that were ignored due to inconsistency with the schema , defined for the DataSet.
You can put an XML document directly into an XmlDataDocument, or you can create it based on a DataSet. The code for the first option might look like this:
Dim objXmlDataDoc As New XmlDataDocument()
objXmlDataDoc.Load(Server.MapPath("file.xml"))
or like this:
objXmlDataDoc.DataSet.ReadXml(Server.MapPath("file.xml"))
There won't be any difference.
Or you can first create a DataSet, fill it with data, and then create an XmlDataDocument based on it:
Dim ds As New DataSet()
'fill in ds
...
Dim objXmlDataDoc As New XmlDataDocument(ds)
Once the XmlDataDocument object is created, you can perform various actions on it:
· bind to DataGrid and other controls:
dg.DataSource = objXmlDataDoc.DataSet
· get the desired string (it is returned as an XmlElement object):
Dim elem As XmlElement
elem = objXmlDataDoc.GetElementFromRow_
(ds.Tables(0).Rows(1))
· use the full set of DOM properties and methods. XmlDataDocument inherits these properties and methods from the XmlDocument object
· apply XSLT transformations (XslTransform objects are used for this purpose).
Learn more about XSLT transformations:
XSLT allows you to transform a source XML document into another document that differs in format and structure. For example, using XSLT, an XML document can be converted to HTML code for display in a Web application. ASP.NET uses the XslTransform class to perform XSLT transformations.
What is it like to work with him?
· to carry out transformations, you first need to create a DataSet and the corresponding XmlDataDocument:
Dim ds As New DataSet()
'fill in DataSet
...
Dim xmlDoc As New XmlDataDocument(ds)
· next action - create an XslTransform object:
Dim xslTran As New XslTransform()
· use the Load method of this object to load the transformation into it:
xslTran.Load(Server.MapPath("PubTitles.xsl"))
· create an XmlTextWriter object (it will be used to display the transformation results):
Dim writer As New XmlTextWriter _
(Server.MapPath("PubTitles_output.html"), _
System.Text.Encoding.UTF8)
· We perform the transformation itself using the Transform method of the XslTransform object. This method has several options. One way to use it might look like this:
xslTran.Transform(xmlDoc, Nothing, writer)
· close the Writer object:
writer.Close()
To work with XML on a Web form, you can do without a DataSet object (and controls designed to display data from a relational source). You can use the XML Web Server Control instead. It allows you to display XML documents themselves or the results of their transformations on a Web page. XML code can be passed to this control in different ways:
· directly open them from disk (via the DocumentSource property). In this case (if you have not applied XSLT transformations), the XML document will be output to the form "as is":
XML Example
TransformSource="MyStyle.xsl" runat="server" />
· open them as objects and pass them to this control (via the Document property). In our case XML Web Server Control is called Xml1:
Private Sub Page_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim xmlDoc As System.Xml.XmlDocument = _
New System.Xml.XmlDocument()
xmlDoc.Load(Server.MapPath("MySource.xml"))
Dim xslTran As System.Xml.Xsl.XslTransform = _
New System.Xml.Xsl.XslTransform()
xslTran.Load(Server.MapPath("MyStyle.xsl"))
Xml1.Document = xmlDoc
Xml1.Transform = xslTran
End Sub
· simply programmatically generate XML code and pass that XML code to Web Server Control (via the DocumentContent property)
· in general, directly enter the XML code into the XML Web Server Control tag:
perform the transformation and transfer the transformation results to it
An example that illustrates all these possibilities is presented below:
Document="XmlDocument object to display"
DocumentContent="String of XML"
DocumentSource="Path to XML Document"
Transform="XslTransform object"
TransformSource="Path to XSL Transform Document"
runat = "server ">
You can add an XML Web Server Control to a Web form by simply dragging this control from the ToolBox or programmatically:
< asp:Xmlid="xmlCtl"runat = "server " />
XmlCtl.Document.Save (Server.MapPath("xmlResult.xml "))
Last updated: 10/31/2015
There are several approaches you can use to work with XML in C#. In the first versions of the framework, the main functionality for working with XML was provided by the System.Xml namespace. It defines a number of classes that allow you to manipulate an xml document:
XmlNode: Represents an xml node. The entire document or a single element can be used as a node
XmlDocument : represents the entire xml document
XmlElement: Represents a single element. Inherited from the XmlNode class
XmlAttribute : Represents an element attribute
XmlText: represents the value of an element as text, that is, the text that appears in the element between its opening and closing tags
XmlComment : represents a comment in xml
XmlNodeList: used to work with a list of nodes
The key class that allows you to manipulate xml content is XmlNode, so let's look at some of its main methods and properties:
The Attributes property returns an XmlAttributeCollection object that represents a collection of attributes
The ChildNodes property returns the collection of child nodes for a given node
The HasChildNodes property returns true if the current node has child nodes
The FirstChild property returns the first child node
The LastChild property returns the last child node
The InnerText property returns the text value of the node
The InnerXml property returns all the internal xml markup of a node
The Name property returns the name of the node. For example,
The ParentNode property returns the parent node of the current node
Let's apply these classes and their functionality. And first, to work with xml, let's create a new file. Let's call him users.xml and define the following content in it:
Now let's go through this document and output its data to the console:
Using System.Xml; class Program ( static void Main(string args) ( XmlDocument xDoc = new XmlDocument(); xDoc.Load("users.xml"); // get the root element XmlElement xRoot = xDoc.DocumentElement; // traverse all nodes in the root element foreach(XmlNode xnode in xRoot) ( // get the name attribute if(xnode.Attributes.Count>0) ( XmlNode attr = xnode.Attributes.GetNamedItem("name"); if (attr!=null) Console.WriteLine(attr .Value); ) // traverse all child nodes of the element user foreach(XmlNode childnode in xnode.ChildNodes) ( // if the node is company if(childnode.Name=="company") ( Console.WriteLine("Company: (0 )", childnode.InnerText); ) // if the node is age if (childnode.Name == "age") ( Console.WriteLine("Age: (0)", childnode.InnerText); ) ) Console.WriteLine() ; ) Console.Read();
As a result, I will get the following output on the console:
To start working with an xml document, we need to create an XmlDocument object and then load an xml file into it: xDoc.Load("users.xml");
When parsing xml, we first get the root element of the document using the xDoc.DocumentElement property. Next, the actual parsing of the document nodes takes place.
In the foreach(XmlNode xnode in xRoot) loop we go through all the child nodes of the root element. Because child nodes represent elements
To determine what kind of node is in front of us, we can compare its name: if(childnode.Name=="company")
In a similar way we can create User objects from xml data:
List
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
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 type XmlTextWriter (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 in 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 entirely 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 current use of XML data is in one way or another World Wide Web, then 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.
Tools for editing, validating, formatting, comparing XML files, as well as support for XQuery, XPath, sitemaps, schemas and RSS feeds
Commonly used abbreviations
- API: Application Programming Interface
- DITA: Darwin Information Typing Architecture
- DTD: Document Type Definition
- HTML: Hypertext Markup Language
- IDE: Integrated Development Environment (integrated development environment)
- URL: Uniform Resource Locator
- W3C: World Wide Web Consortium (WWW consortium)
- WSDL: Web Services Description Language
- XHTML: Extensible Hypertext Markup Language
- XML: Extensible Markup Language
- XSLT: Extensible Stylesheet Language Transformations
When choosing tools to work with XML technologies, you first need to determine the requirements. For example, if you plan to perform various tasks with XML (editing, validation, etc.), consider using an XML IDE with the appropriate functionality. For a specific task (comparing XML files or creating a sitemap), consider using a more specialized tool for that specific task.
This article covers the following categories of XML tools:
- Programs for developing and checking XML sitemaps.
- RSS feed generators.
- XML schema generators.
- Programs for checking the correctness of XML.
- XML formatting programs.
- XML editors.
- Tools for working with XML.
- Open source tools for working with XML.
- XML Integrated Development Environments.
- XML comparison tools
- Tools for working with XQuery.
- Tools for working with XPath.
Program for creating XML sitemaps
An XML sitemap is a list of all the URLs of a Web site. A sitemap informs a search engine of the Web site URLs that are available for crawling and inclusion in the search engine's database. Most sitemap programs are Web-based and ask for the URL of a Web site along with several parameters such as frequency of changes and date of last modification. Links to all listed tools are provided in the section.
There are several sitemap generators:
- Google SiteMap Generator automatically generates a sitemap based on updates and traffic to your Web site deployed on the Web server.
- Gsite Crawler is a Windows application for creating sitemaps.
- In addition to downloadable tools, sitemaps can be generated using a variety of interactive applications; here are two examples:
- Sitemaps Builder creates sitemaps for Google, HTML and text URLs.
- XML Sitemaps creates site maps in XML, ROR, Text or HTML formats.
Programs for checking the correctness of XML sitemaps
Sitemap validation programs are used to validate maps generated for Web sites. Such a program checks whether the sitemap is suitable for use by a search engine. Links to all listed tools are provided in the section.
Below is a list of sitemap checking programs:
- Automapit sitemap validator checks the correctness of the site map, ensuring that it meets search engine criteria.
- Sitemap XML validator checks your sitemap for correct XML code so you can correct errors before sending it to search engines.
- XML sitemaps validator identifies any map problems so they can be resolved before submitting the map to search engines.
- Online Merchant sitemap checker checks the correctness of XML headers in the sitemap.xml file.
RSS feed generators
RSS news feeds are a great way to keep your site visitors up to date with the latest content changes. RSS feed generators are popular, for example, among users who want to view headlines from news sites (such as CNN) or know about the latest sports news. Links to all listed tools are provided in the section.
Web site developers can generate RSS feeds using the following tools:
- IceRocket RSS builder is a program with a simple interface that allows you to add topics, links and content to create RSS feeds for your Web site.
- Feedity creates RSS feeds for Web pages, news or products.
- RSSPect Sets up RSS feeds for Web sites, documents, or podcasts.
XML Schema Generators
XML schemas can be generated from an XML instance. Links to all listed tools are provided in the section.
Available tools:
- Trang by ThaiOpenSource– a command-line program that generates an XML Schema Definition (XSD) from XML.
- XMLBeans is a program from the Apache project that provides several functions, including schema generation using inst2xsd (Instance to Schema Tool).
- XML for ASP BuildXMLSchema– interactive XML schema generator.
Programs for checking the correctness of XML
XML code instances can be checked against their schemas. Links to all listed tools are provided in the section.
Use one of the following interactive tools:
- XMLValidation.com checks that an XML document matches the XML schema or DTD specified in the document, or, if there is no schema or DTD declaration, checks the syntax.
- DecisionSoft.com Schema Validator checks the correctness of one schema and document instance and displays a list of errors.
- W3C XML validator– service for checking schemas with the namespace URI http://www.w3.org/2001/XMLSchema.
XML formatters
XML formatting is an operation often performed on XML data to make it easier to read. Most desktop applications that work with XML provide this functionality. To quickly format XML content without installing any tools, try one of the following online services. Links to all listed tools are provided in the section.
- XMLIndent.com
- X01 online xml formatter
XML editors
XML editors help interpret an XML document by highlighting elements, attributes, or plain text and indentation. Another advantage of using XML editors is that they have content-oriented features, such as a tree view, which allows the user to easily view the various nodes of an XML document. They also check the correctness of the code and display a list of warnings and errors if XML closing tags are missing. Links to all listed tools are provided in the section.
- Xerlin XML Editor– A Java™ program that creates and validates XML content. The editor is open source, supports XSLT, and can validate XML against DTDs and schemas.
- Jax Editor is another open source XML editor in Java. Supports content export to PDF and HTML preview using XSLT; works on multiple platforms.
- XMLFox is a free XML editor with a proofreader that allows you to create grammatically correct XML documents and schemas. This editor also supports other XML operations.
XML Tools
XSLT transformations are useful when transforming one XML form into another using style sheets. Numerous tools can help with this; examples include Tiger XSLT Mapper and Kernow. Links to all listed tools are provided in the section.
Tiger XSLT Mapper is an XML structure conversion program that can be easily used by beginners. It automatically creates transformations that you can edit using a drag-and-drop graphical interface.
Kernow is a Java interface that performs transformations programmatically. Kernow is a good choice for developers who need to periodically perform XSLT transformations using a visual interface.
Several interactive XSLT programs are also useful:
- XSLT Online Transformation
- W3C Online XSLT 2.0 Service
Developers who prefer browser plugins can use the following useful XML plugins:
Mozilla Firefox
- XSL Results Add-on displays the results of an XSLT transformation (XSLT 1.0 or XSLT 2.0 through Saxon-B) of a document.
- XML Developer Toolbar adds standard tools for working with XML to the toolbar.
Google Chrome
- XML Tree displays XML data in a user-friendly format.
- XML Viewer– XML document viewer for Google Chrome.
Open source tools for working with XML
Users who cannot purchase commercial XML tools will benefit from open source tools. The active work of the community has led to the creation of very good programs for working with XML. Links to all listed tools are provided in the section.
XML IDE iXedit provides a number of functionality for processing XML documents:
- DTD compliance check.
- Automatic code completion using DTD.
- Custom templates.
- XSLT processing.
- Editing in parts.
Rinzo XML Editor is an XML editor that runs in Eclipse. Among its features:
- Namespace support.
- Automatic completion of tags and attributes.
- XML Validation.
These tools also provide capabilities for working with Java elements:
- Automatic completion of class names.
- Opening a class definition.
XPontus XML Editor is an open source Java program with the following functionality:
- Code formatting and completion.
- XSL transformations.
- Generating DTD and Schema.
- XML Validation.
XML IDEs
XML integrated development environments (IDEs) handle almost all XML-related operations. There are a number of IDEs available with varying functionality. Links to all listed tools are provided in the section.
XMLSpy is an XML IDE for creating, editing and debugging XML, XML Schemas, XSL/XSLT, XQuery, WSDL and SOAP. Additional features:
- Code generator.
- File converter.
- Debugger.
- Profiler.
- Supports integration into Visual Studio.NET and Eclipse IDE.
- Database Import Wizard, which allows you to import data from Microsoft® Access®.
XML Marker is an XML editor that uses a synchronized table-tree and text display to display XML data in tabular and hierarchical form. This program can download very large documents (hundreds of megabytes and even several gigabytes in size). Other functionality:
- Editor with syntax highlighting.
- Sorting tables.
- Automatic indentation.
- Syntax checking as you type.
Liquid XML Studio– a package of several programs for working with XML. Provides the following tools:
- XML Schema editor.
- XML data binding code generator.
- WSDL editor.
- XML editor.
- Integration with Microsoft Visual Studio.
- Web services test client.
- XPath Expression Composer.
- HTML documentation generator.
- XSLT editor and debugger.
- Large file editor.
- XML Diff – comparison of XML files.
- Intelligent XML editor.
- XML Validation.
- XSL/XSLT support.
- XQuery support.
- XPath support.
- XML publishing from a single source.
- Support for Microsoft Office documents.
Stylus Studio offers the following functionality:
- XSLT and XQuery profilers.
- EDI support.
- Tools for working with corporate Web services.
- XML pipeline.
- Interaction of XML Schema with XSLT 2.0 and XQuery 1.0.
- XML publishing tools.
XML Notepad from Microsoft helps developers create XML documents. This free program includes a tool called XMLDiff that you can use to compare two XML files. Its interface is simple and easy to use. The program runs on the .Net platform. Here are some of its features:
- Tree view synchronized with text view.
- Namespace support when copying and moving text.
- Incremental search in tree and text views.
- Drag and drop editing support.
- Unlimited number of operations to undo changes and redo changes when editing.
- Support for searching using regular expressions and XPath.
- Instant check for XML schema compliance.
- Intellisense technology (autocompletion), based on expected elements, attributes and values of enumerated simple types.
- Support for specialized editors for data types date, dateTime, time and other types, for example, color.
- Built-in HTML viewer.
- XInclude support.
XML Copy Editor is a fast XML editor with support for validation. The presence of tabs allows you to edit several files at the same time. Other functionality:
- Checking for DTD/XML Schema/RELAX NG compliance.
- XSLT and XPath support.
- Formatted print output and color syntax highlighting.
- Collapse and completion of tags.
- Import and export Microsoft Word documents without losing information.
- Support for XHTML, XSL, DocBook and Text Encoding Initiative (TEI).
firstobject XML Editor– free XML editor. Allows you to directly edit the XML tree displayed based on the content of the XML document. Allows you to download and process large files. Some of its editor functionality:
- Fast, portable, based on CMarkup.
- Does not require Java or MSXML.
- Line wrap support.
- MSXML-based DTD compliance check.
- Go to line by number.
- XPath mapping.
- Editing files in bookmarks.
- Generating C++ code.
XRay XML Editor– free integrated XML IDE. This program checks the validity of the XML document as it is entered. Has built-in support for W3C standards. Also has an HTML viewer for previewing Web pages created using XML. You can create three types of schemas, including XSD, DTD, and External Data Representation (XDR). Other functionality includes:
- Real-time XSLT processing.
- Checking the correctness of the scheme in real time.
- Integrated online XML tutorial.
XMLSpear is a free XML editor written in Java and available for many platforms. Features advanced functionality such as interactive schema validation, advanced XPath panel, and more. XML is displayed in three different formats, including tree view, element view, and source view. XMLSpear is available as a Java Web program or as a standalone application. Additional functionality:
- XPath and XSLT support.
- Ability to generate complete XML documents from a schema.
- Supports multiple encoding formats.
- Integrated text and HTML plugin.
- Check against schema or DTD in real time.
- Generating schema from XML instances.
- Tree editor for working with nodes.
XMLmind is a feature-rich XML editor using Java technology and available for multiple platforms. More suitable for experienced users rather than beginners. Introduces an innovative way to edit XML documents and requires the Java platform. XMLmind features include:
- Convert XML documents to HTML, PDF, Eclipse and many other formats.
- Availability of DITA converter.
- Support for DocBook, JavaDoc, XHTML and built-in templates for them.
- Support for creating MathML documents.
- Editable commands.
- Integrated XML parser and XSLT engine.
ElfData XML Editor– a program for Mac OS users. This XML IDE has Unicode support and can perform grammatical checks on XML documents with and without a DTD. Two presentation modes are available: tree view and code view. Drag-and-drop technology support allows you to drag XML elements onto a document. The search is made easier by the presence of two modes: code search and tree search. Other functionality includes:
- XML 1.0 compliant.
- Macintosh style user interface.
- Detailed error messages with assistance in debugging them.
- The "Send to Browser" function allows you to view a document in a browser.
- Ability to save pages as XHTML with DTD.
XMetaL similar to a word processor. Like most XML IDEs, it can perform validation of XML documents and supports schemas, DTDs, and XIncludes. Other functionality:
- Spell checking and automatic code completion.
- Support for output in Web help format.
- Ability to convert XML documents to PDF, HTML and many other formats.
- The XMetal connector integrates with content management systems (CMS) and version control systems such as SVN.
- Unicode support allows you to create XML documents in various languages.
- DITA support (with features such as visualization and theme-driven user interface) for creating DITA content.
XML comparison tools
Developers, editors, and authors often need a program to compare two versions of an XML document to track changes. Although there are numerous comparison tools available, the most effective solution for many operations is a comparison program specifically designed for working with XML documents. Links to all listed tools are provided in the section.
- Timestamp.
- Content.
- Binary comparison.
Liquid XMLDiff has many XML-specific features, such as removing whitespace, comments, and processor directives. This program is powerful enough to predict whether items are new, deleted, or moved. The program is also available as part of Liquid XML Studio in a designer and developer edition.
ExamXML is a powerful tool for visually comparing and synchronizing differences between XML documents. The XML input for comparison can be either a file or a field from a database. ExamXML can also compare and store parts of an XML document; You can also import or export from Microsoft Excel® documents. ExamXML runs on various versions of Microsoft Windows®. Other functionality:
- Checking the correctness of XML for compliance with the DTD and XML Schema.
- Normalization of dates and numbers.
- Drag-and-drop support.
- XML documents are displayed as a tree.
DeltaXML allows you to search, compare, connect and synchronize changes in XML documents. Supports the Java API, making it easier to programmatically compare XML documents. Can work with large files. The program can produce a delta file with the comparison results. This file can be displayed directly or using XSL; you can process this file in other XMKL programs. DeltaXML Sync can compare three XML documents and visualize the differences. In addition to the XML document comparison function, it has several formatting tools:
- DeltaXML DITA comparison.
- Comparison of DeltaXML DocBook.
- DeltaXML ODT comparison.
- Merge DeltaXML ODT.
Tools for working with XQuery
The XQuery language can be very useful for advanced XML users to query and extract content from large XML documents. XQuery-specific programs can help you take advantage of XQuery and enable high-level features such as display, debugging, and profiling. These programs provide useful features such as correctness checking, code completion, and preview. Links to all listed tools are provided in the section.
Editor XMLSpy XQuery Editor provides syntax highlighting and context-sensitive menus for XQuery. Its code completion features make it easy to create XQuery documents. It also allows you to develop XQuery for XML-aware databases. Other functionality:
- Error isolation.
- Simplified debugging.
- Improved code performance.
- Improved text viewing.
Program Stylus Studio XQuery Editor has an integrated XQuery editor with a wide range of functionality, including smart code checking, code completion, element constructors, functions, path expressions and much more. The editor is based on the open XQuery architecture with support for the Saxon XQuery processor. The XQuery source tree window supports drag-and-drop technology and convenient symbols and icons. Additional functionality:
- Creating XQuery scripts.
- XQuery Preview.
- Mapping XQuery results to XQuery expressions.
XQuery Development Tools for Eclipse Help you create, debug, and run XQuery in the Eclipse environment. These tools also provide:
- Support for XQuery updates and scripting enhancements.
- Code completion and code patterns.
- Semantics check and quick fixes.
- Validation of input when entering.
XPath Tools
Specialized XPath tools are useful for visualizing the results of XPath evaluation and assist in creating and validating XPath expressions. Useful features provided by these tools include debugging XPath expressions, automatic code completion, and searching the database using XPath. Links to all listed tools are provided in the section.
Program SketchPath is an XPath editor and XML parsing and testing tool. It provides an IDE for developing and testing XPath expressions against XML documents. The program uses the .NET Framework to work with XPath 1.0 and Saxon.NET for XPath 2.0. Other functionality:
- Using XPath variables in expressions.
- XPath Function Helper.
- Built-in step tracer and debugger.
- Syntax color highlighting for expressions.
XPath Visualizer is a free Microsoft Windows program that performs XPath queries on XML documents and visualizes the results. The input can be a file on the file system or a URL; You can also copy the contents of the file directly into the program as text. You can enter full XPath queries in the program. Other functionality:
- Automatic detection and mapping of XML namespaces.
- Checking the correctness of XPath queries.
- Automatically add a default XML namespace to a query expression and the ability to remove an XML namespace from any document.
Web programs for working with XPath are also available, for example:
- XPath Query Expression Tool(XMLME.com).
- Simple online XPath tester.
- XSLT Tryit Editor(W3Schools.com).
Conclusion
XML technology is supported in many tools. As an XML user, you should analyze your needs and select the appropriate tool. For example, if you need to perform many different operations, you can choose an XML IDE with additional functionality such as editing, validation, etc. For narrow tasks (for example, comparing XML files), you can choose a specialized comparison program.