Easily process XML data in the. NET Framework (ii)

Source: Internet
Author: User
Tags date object contains sql net string tag name valid
xml| data

Analysis attribute value

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 attribute value is February 8, 2001, and if you convert the string to the DateTime type in the. NET framework using the System.Convert class, 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.

Processing XML text (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. Include SQL Server? And Microsoft Office, these applications 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 of this 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");

Spaces in an XML document are important or unimportant. 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:





£???

£

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

Through the whitespacehandling properties of the XmlTextReader class you can handle spaces. 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".

string and Fragment

The programmer cuts down the program in MSXML and finds that there is a big difference between the COM and. NET Framework XML APIs. The. NET Framework class itself does not provide a way to parse XML data stored in strings. Unlike the MSXML parser object, the Xmltestreader class does not provide any kind of loadxml method to create a reader from a well-formed character. There are no methods like Loadxml because you can use special text reader---StringReader class to get the same functionality.

xmltextreader one of the constructors accepts a TextReader derived object and an XML reader as an argument (the reader is created based on the contents of text reader). A text reader class is a stream that the input character is optimized to generate. The StringReader class inherits the TextReader class and uses an in-memory string as its input stream. The following code fragment demonstrates how to initialize an XML reader with a well-formed XML string as its input:

string XmlText = "...";

stringreader Strreader = new StringReader (xmlText);

xmltextreader reader = new XmlTextReader (Strreader);

In addition, using the StringWriter class instead of the Textwrite class, you can create an XML document from the memory character.

An XML string of a specified type is an XML fragment (fragment). XML fragments are composed of XML literals, but XML documents without root nodes are not well-formed XML documents and cannot be applied. An XML fragment is part of the original document, so it may be missing a root node. For example, the following XML literal is a valid XML fragment, but is not a valid XML document because it does not have a root node:

dino

esposito

The .net Framework XML API allows programmers to use XML fragments in conjunction with a parser content, which consists of similar encoding character sets, DTD documents, namespaces, languages, and space handlers:

public XmlTextReader (

string XmlFragment,

xmlnodetype Fragtype,

xmlparsercontext context

£);

The xmlfragment parameter includes XML string parsing. The Fragtype parameter represents the type of the fragment, which gives the type of the fragment root node. Only the Element,attibute and document type nodes can be the root node of the fragment, and the contents of the parser can be interpreted by the XmlParserContext class.

Author: Chyich (translated)/aspcool



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.