The. NET framework handles XML data easily (ii)

Source: Internet
Author: User
Tags tag name valid

Parsing property values

In most cases, the property value is a simple text string. However, this does not mean that the property values in the actual application are character-type. Sometimes, a property value is a combination of many types of data, such as Date or Boolean, at which point you will use the XmlConvert or System.convevt class method to convert these types to their original types. The XmlConvert and SYSTEM.CONVEVT classes can implement conversions of data types, but the XmlConvert class is converted according to the data type specified in the XSD, regardless of what type it is now.

Suppose you have the following XML data fragment:

Let's first confirm that the Birthdaay property value is february8,2001 if you convert the string to the DateTime type in the. NETFramework with the System.Convert class, so that we can use it as a date type. In contrast, if you use the XmlConvert class to convert a string, you will see an parsing error because the XmlConvert class does not correctly interpret the date in the string. Because in XML, date-type data must be formatted in YYYY-MM-DD form. The XmlConvert class acts as a reciprocal conversion between CLR types and XSD types. The conversion results are local when the conversion takes place.

In some solutions, property values are composed of plain text and entities. Of all the reader classes, only the XmlValidatingReader class can handle the entity. XmlTextReader cannot handle entities, but when they appear in attribute values, it can only take text values out. In this case, you must use the Readattributevalue method instead of the simple Read method to analyze the contents of the property value.

The Readattributevalue method analyzes attribute values and then separates the constituent elements (such as separating plain text from entities). You can use the return value of the Readattributevalue method as a looping condition to iterate through the elements of the entire property value. Now that the XmlTextReader class cannot handle entities, you can write a class for the entity you are working on yourself. The following code fragment demonstrates how to invoke a custom processing class:

while(reader.ReadAttributeValue())
{
if (reader.NodeType == XmlNodeType.EntityReference)
// Resolve the "reader.Name" reference and add
// the result to a buffer
buf += YourResolverCode(reader.Name);
else
// Just append the value to the buffer
buf += reader.Value;
}

When the property value is all parsed, the Readatributevalue method returns False, which ends the loop. The final result of the property value is the value of the global variable, buffer.

Working with XML literals (text)

When we are dealing with XML tag text, if we do not handle it correctly, the cause of the error can be determined quickly. For example, a character conversion error, it must be transmitted by the non-XML text into an XML data stream. Not all characters that are valid in a given platform are valid XML characters. Only valid characters specified in the XML specification (www.w3.org/TR/2000/REC-xml-20001006.html) can be used safely as element and attribute names.

The XmlConvert class provides the ability to convert the name of a non-XML standard to a standard XML naming function. When the tag name contains invalid XML characters, the EncodeName and Decodename methods can adjust them to a schema-compliant XML name. Includes SQL Server? and MicrosoftOffice, which allow and support Unicode documents, however, some of the characters in these documents are not valid XML names. The typical situation is when you are working on a column name that contains spaces in your database. Although SQL Server allows long column names, this may not be a valid name for an XML stream. Spaces are replaced by hexadecimal code invoice_0x0020_details. The following code shows how to get the string in a program:

Xmlconvert.encodename ("Invoice Details");

The opposite method is decodename. This method converts the XML text into its original format. Note that it only converts the complete hexadecimal code, only _0x0020_ is treated as a space, and _0x20_ is not:

Xmlconvert.decodename ("Invoice_0x0020_details");

The spaces in the XML document are important and not important. It is important to say that it is meaningful when it appears in the content of an element or when it is in an annotation statement. For example, the following situation:

<MyNode xml:space="preserve">
<!-- any space here must be preserved -->
???
</MyNode>

In XML, spaces are not just for spaces (blanks), but also for carriage returns, newline, and indentation.

You can work with spaces by using the WhitespaceHandling property of the XmlTextReader class. This property accepts and returns a WhitespaceHandling enumeration value (the enumeration class has three optional values). The default value is all, which means that meaningful and meaningless spaces are returned as nodes----significantwhitespace and whitespace nodes respectively. The other enumeration value is none, which means that no space is returned as a node. Finally, the Signficant enumeration value, which represents ignoring meaningless spaces and returning only nodes whose node type is signficantwhitespace. Note the WhitespaceHandling property is one of the few reader properties. It can be changed at any time and have an impact on the read operation. And the normalization and XmlResolver attributes are "sensitive".

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.