Appendix V
XML

This appendix summarizes useful XML topics and techniques.

Special Characters

The following table lists five special characters defined for use in XML files.

CharacterCode
<<
>>
&&
''
""

You can also include special characters inside CDATA sections. A CDATA section begins with <![CDATA[ and includes all the following text until it reaches the closing sequence ]]>. The CDATA can include carriage returns, ampersands, quotes, and other special characters.

Writing XML Data

The .NET Framework provides two main ways to write XML data: the XmlWriter class and the XML Document Object Model. The following two sections describe these approaches.

XmlWriter

The XmlWriter class provides methods for writing the pieces of an XML file. To use an XmlWriter to write XML data into a file, call the class’s Create method to create the file. Then use the other methods to write the pieces of the XML document into the file.

The following table lists the most useful XmlWriter methods.

MethodPurpose
CloseCloses the writer’s underlying stream.
CreateCreates an XmlWriter associated with a file, stream, StringBuilder, or other object.
DisposeFrees the writer’s resources. (You can use the using statement to ensure that the writer is disposed.)
FlushFlushes output to the underlying stream.
WriteAttributeStringWrites an attribute with a specified name and value.
WriteCDataWrites CDATA.
WriteCommentWrites a comment.
WriteElementStringWrites an element with a specified name and text value.
WriteEndAttributeEnds an attribute started with WriteStartAttribute.
WriteEndDocumentEnds the document.
WriteNameWrites a name.
WriteStartAttributeStarts an attribute.
WriteStartDocumentStarts the document.
WriteStartElementStarts an element.
WriteStringWrites a string, escaping special characters such as < and > if necessary.
WriteValueWrites a value such as a bool, int, or double.

The XmlTextWriter class, which inherits from XmlWriter, works much as XmlWriter does but it can produce nicely indented output. Simply create an XmlTextWriter instead of an XmlWriter, set its Formatting property to Indented, and create the XML file using the methods described in the preceding table.

Document Object Model

The XML Document Object Model (DOM) provides a more structured way to build XML documents. It uses objects to create an in-memory model of an XML document. You can then manipulate the model and save the result into an XML file or string.

Two of the most important classes for manipulating the DOM are XDocument and XElement.

The XDocument class represents an XML document. Its most useful properties are Declaration, which gets or sets the document’s XML declaration, and Root, which returns the document’s root element.

The following table lists the XDocument class’s most useful methods.

MethodPurpose
AddAdds an item to the document’s child collection. (Note that you can add only one child, the root element, to the document.)
DescendantNodesReturns a collection of XNode objects that are descendants of the document.
DescendantsReturns a collection of XElement objects that are descendants of the document. If you specify a name, the method returns only elements with that name.
LoadLoads the document from a filename, stream, or XmlReader.
ParseCreates a new XDocument from an XML string.
SaveSaves the document into a file, stream, or writer.
ToStringReturns the document’s indented XML code.
WriteToWrites the document into an XmlWriter.

The XElement class represents an element in an XML document. The following table lists the XElement class’s most useful properties.

PropertyPurpose
DocumentReturns the XDocument that contains the element.
FirstAttributeGets the element’s first attribute.
FirstNodeGets the element’s first child node.
HasAttributesReturns true if the element has attributes.
HasElementsReturns true if the element has child elements.
IsEmptyReturns true if the element contains no content. (It still might have attributes.)
LastAttributeGets the element’s last attribute.
LastNodeGets the element’s last child node.
NameGets or sets the element’s name.
NextNodeReturns the next node in the element’s parent’s child list.
NodeTypeGets the node’s type.
ParentGets the element’s parent element.
PreviousNodeReturns the previous node in the element’s parent’s child list.
ValueGets or sets the node’s text contents.

The following table lists the XElement class’s most useful methods.

MethodPurpose
AddAdds an item at the end of the element’s child collection.
AddAfterSelfAdds an item to the parent’s child collection after this element.
AddBeforeSelfAdds an item to the parent’s child collection before this element.
AddFirstAdds an item at the beginning of the element’s child collection.
AncestorsReturns a collection of XElement objects that are ancestors of the element. If you specify a name, the method returns only elements with that name.
AttributeReturns an attribute with a specific name.
AttributesReturns a collection containing this element’s attributes. If you specify a name, the collection includes only attributes with that name.
DescendantNodesReturns a collection of XNode objects that are descendants of the element.
DescendantsReturns a collection of XElement objects that are descendants of the element. If you specify a name, the method returns only elements with that name.
DescendantsAndSelfReturns a collection of XElement objects that includes this element and its descendants. If you specify a name, the method returns only elements with that name.
ElementReturns the first child element with a specified name.
ElementsReturns a collection holding the element’s children. If you specify a name, the method returns only elements with that name.
ElementsAfterSelfReturns a collection holding the element’s siblings that come after this element. If you specify a name, the method returns only elements with that name.
ElementsBeforeSelfReturns a collection holding the element’s siblings that come before this element. If you specify a name, the method returns only elements with that name.
IsAfterReturns true if this node comes after another specified node in a document.
IsBeforeReturns true if this node comes before another specified node in a document.
LoadLoads the element from a filename, stream, or reader.
NodesReturns a collection holding this element’s child nodes.
NodesAfterSelfReturns a collection holding the node’s siblings that come after this node.
NodesBeforeSelfReturns a collection holding the node’s siblings that come before this node.
ParseCreates an XElement from an XML string.
RemoveRemoves this element from its parent.
RemoveAllRemoves all nodes and attributes from this element.
RemoveAttributesRemoves this element’s attributes.
RemoveNodesRemoves this element’s child nodes.
ReplaceAllReplaces the element’s child nodes and attributes with specified new ones.
ReplaceAttributesReplaces the element’s attributes with specified new ones.
ReplaceNodesReplaces the element’s child nodes with specified new ones.
ReplaceWithReplaces this node with new specified content.
SaveSaves the element into a file, stream, or writer.
SetAttributeValueSets, adds, or removes an attribute.
SetElementValueSets, adds, or removes a child element.
SetValueSets the element’s value.
ToStringReturns the element’s indented XML code.
WriteToWrites the element into an XmlWriter.

XML Literals

C# doesn’t support XML literals but it does support multiline string literals and they work almost as well. Simply use the XElement.Parse method to parse a multiline string holding the XML code, as in the following example.

XElement student = XElement.Parse(
    @"<Student>
        <FirstName>Arthur</FirstName>
        <LastName>Andrews</LastName>
        <StudentID>83746</StudentID>
    </Student>");

Reading XML Data

The following two sections explain how you can use the XmlTextReader class and the Document Object Model to read XML code.

XmlTextReader

The XmlTextReader class provides fast, forward-only, noncached methods for reading XML data. It provides methods to move through an XML file one node at a time and to examine the data provided by each node.

To use an XmlTextReader, use the class’s constructor or the XmlReader class’s static Create method to create an object associated with the file or input stream that you want to read. Use the object’s Read method to read the next node from the XML data. After you read a node, you can use the reader’s properties and methods to determine the node’s name, type, attributes, content, and other properties.

The following table lists the most useful XmlReader properties.

PropertyMeaning
AttributeCountReturns the number of attributes the node has.
DepthReturns the depth of the current node in the XML hierarchy.
EOFReturns true when the reader is at the end of the XML data.
HasAttributesReturns true if the node has attributes.
HasValueReturns true if the node can have a value.
IsEmptyElementReturns true if the node is an empty element as in <Overdue />.
ItemGets a node attribute by index or name. (This is the class’s indexer, so you use it as in reader[0] instead of invoking the Item property explicitly.)
NameReturns the node’s name.
ValueReturns the text value of the current node.

The following table lists the XmlReader class’s most useful methods.

MethodPurpose
CreateCreates a new reader associated with a string, stream, file, or other data source.
DisposeFrees the object’s resources. You can include a using statement to automatically call Dispose.
GetAttributeGets an attribute for the current node by index or name. (Similar to the Item property.)
IsNameReturns true if its parameter is a valid XML name.
MoveToAttributeMoves the reader to an attribute specified by index or name.
MoveToContentMoves the reader to the current node’s content.
MoveToElementMoves the reader to the element containing the reader’s current position. For example, if you move the reader to examine an element’s attributes, the method moves the reader back to the element’s node.
MoveToFirstAttributeMoves the reader to the current node’s first attribute node.
MoveToNextAttributeMoves the reader to the current node’s next attribute node.
ReadReads the next node from the XML data.
ReadInnerXmlReturns the current node’s descendants as an XML string.
ReadOuterXmlReturns the current node’s subtree (including the current node) as an XML string.
ReadToDescendantMoves the reader to a subelement with a specified name.
ReadToNextSiblingMoves the reader past the rest of the current node to its next sibling.
SkipSkips the current node’s children.

Document Object Model

You can use the DOM to load and study an existing XML file.

To load an object model from XML data, simply call the XDocument class’s static Load method, passing it a filename, stream, or XmlReader.

Related Technologies

The following list summarizes some XML-related technologies.

  • XSL (Extensible Stylesheet Language)—This refers to a family of languages and tools for reformatting XML data. It includes
    • XSLT (XSL Transform)—A language for transforming XML data into other formats such as plain text, HTML, rearranged XML documents, or XSL FO.
    • XSL FO (XSL Formatting Objects)—A language for formatting XML data for output for screen, PDFs, printers, and so forth.
    • XPath—This is a query language used by XSL and other XML tools to find and identify items within XML data.
  • XQuery—A somewhat SQL-like language for querying XML data.
  • DTD (Document Type Definition)—An XML data file validation language. You use DTD to define the required structure of an XML file. Then you can validate a particular XML file to see if it satisfies those requirements.
  • XSD (XML Schema Definition)—Another XML data file validation language. See DTD.
  • XLink—A language for defining hyperlinks in XML data.
  • SOAP (Simple Object Access Protocol)—A protocol that lets applications (often running on different computers) exchange data.
  • WSDL (Web Services Definition Language)—A language for describing web services.
  • RSS (Really Simple Syndication)—A format for XML news feeds and sites that post news-like items.

The following sections provide more information about XPath and XSLT.

XPath

XPath is a language for identifying items in XML data. An XPath query looks vaguely like a file’s pathname in a directory hierarchy. The query can also include operators that work as wildcards, filter the results, and specify relationships among nodes.

The following table lists the most useful operators that you can use in an XPath query.

OperatorMeaning
/Selects an immediate child.
//Selects descendants.
.The current node.
..The current node’s parent.
*Matches anything.
@Attribute prefix for matching an attribute. For example, @Cost matches an attribute named Cost.
@*Selects all attributes.
()Groups operations.
[ ]Applies a filter. For example, //Planet[@Name="Earth"] matches Planet elements that have a Name attribute with value Earth.
[ ]Subscript operator for accessing items in a collection.
+Addition.
-Subtraction.
DivFloating-point division.
*Multiplication.
ModModulus.

When a query filters results, it can include the boolean and comparison operators listed in the following table.

OperatorMeaning
AndLogical AND
OrLogical OR
not()Logical NOT
=Equals
!=Not equals
<Less than
<=Less than or equal
>Greater than
>=Greater than or equal

For more information on XPath, see the following links.

XSLT

XSLT is a language that you can use to transform XML data into a new format. It’s a fairly complicated language, so there isn’t room to cover it in any depth here. To learn the language, see the following links.

For an example, see the section “XSLT” in Chapter 24, “XML.”

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.19.64.78