Easy XML data processing in. NETFramework (2-1)

Source: Internet
Author: User
?? Analysis attribute value ?? In most cases, the attribute value is a simple text string. However, this does not mean that the attribute values used in actual use are of the stable type. Sometimes, attribute values are composed of many types of data ,?? Analyze attribute values

?? In most cases, the attribute value is a simple text string. However, this does not mean that the attribute values used in actual use are of the stable type. Sometimes, attribute values are composed of many types of data, such as Date or Boolean. in this case, you need to use XmlConvert or System. the Convevt class method converts these types to the types at the cost. Both the XmlConvert and System. Convevt classes can convert data types. However, the XmlConvert class converts data types specified in XSD regardless of the current type.

?? Suppose you have the following XML data snippets:

??

?? Let's first confirm that the value of the birthdaay attribute is February 8, 2001. if you use System. the Convert class converts the string.. NET Framework. in this way, we can use it as the date type. In contrast, if you use the XmlConvert class to convert a string, you will see an analysis error because the XmlConvert class cannot accurately describe the date in the string. Because in XML, the pattern of date type data must be YYYY-MM-DD situation. The XmlConvert class converts CLR and XSD types. When a conversion task is generated, the conversion result is partial.

?? In some solutions, attribute values are composed of plain text and entities. Among all browser classes, only the XmlValidatingReader class can process objects. Although XmlTextReader cannot process objects, it can only extract text values when they are simultaneously presented in the attribute values. In this case, you must use the ReadAttributeValue method to replace the simple read method to analyze the attribute value content.

?? The ReadAttributeValue method analyzes the attribute values and then separates the elements (for example, separating plain text and entity ). You can use the return value of the ReadAttributeValue method as the cyclic condition to traverse all elements in the attribute values. Since the XmlTextReader class cannot process objects, you can write a class for processing objects by yourself. The following code snippet demonstrates how to call 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;

??}

?? After all attribute values are analyzed, the ReadAtributeValue method returns False to end the loop. The final result of the attribute value is the value of the global variable buffer.

?? Processing XML Text (Text)

?? When we are dealing with XML label text, if we cannot accurately handle it, the cause of its error can be quickly determined. For example, if a character conversion error occurs, it must transmit non-XML text to an XML data stream. Not all valid characters in a given platform are valid XML characters. Only valid characters specified in XML specifications (www.w3.org/TR/2000/REC-xml-20001006.html) can be securely used as element and attribute names.

?? The XmlConvert class provides the function of converting a non-XML scale name into a standard XML name. When the tag name contains invalid XML characters, the EncodeName and DecodeName methods can convert them into XML names conforming to the Schema. Contains SQL Server™And Microsoft Office, which use programs to promise and support Unicode documents. However, some of the characters in these documents are not effectively named in XML. The typical case is when you process columns that contain spaces in the database. Although SQL Server promises a long column name, it may not be a valid name for the XML stream. The space is changed by the hexadecimal code Invoice_0x0020_Details. The following code demonstrates how to obtain the string in a program:

?? XmlConvert. EncodeName ('Invoice details ');

?? The opposite method is DecodeName. This method converts XML text into its original structure. Note that it can only convert the complete hexadecimal code. only _ 0x0020 _ is treated as a space, and _ 0x20 _ is not:

?? XmlConvert. DecodeName ('Invoice _ 0x0020_Details ');

?? Spaces in XML documents are also important. It is important when it is presented in the content of an element or in a comment statement, it can represent the actual meaning. For example:



?? <MyNode xml: space = 'preserv'>

?? <! -- Any space here must be preserved -->

?? •••

?? </MyNode>

?? In xml, spaces not only represent spaces (vacancies), but also carriage return, line feed, and indentation.

?? You can use the WhiteSpaceHandling attribute of the XmlTextReader class to process spaces. This property receives and returns a WhiteSpaceHandling enumerated value (this enumeration class has three optional values ). The default value is All, which indicates that meaningful and meaningless spaces are returned as nodes ---- they are distinguished as SignificantWhitespace and Whitespace nodes. The other enumerated value is None, which means that no space is returned as a node. Finally, it is the Signficant enumeration value, which represents a space that is neglected and meaningless, and only returns a node with the node type SignficantWhitespace. Note that the WhiteSpaceHandling attribute is one of a few browser attributes. It can be transformed at any time and has an impact on Read control. 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.