Home > Java > javaTutorial > body text

Easily process XML data in .NET Framework (2-1)

黄舟
Release: 2016-12-20 14:05:48
Original
1232 people have browsed it

??Analyzing attribute values ​​

??In most cases, the attribute value is a simple text string. However, this does not mean that all attribute values ​​in actual applications are character type. Sometimes, the attribute value is a combination of many types of data, such as Date or Boolean. In this case, you have to use the XmlConvert or System.Convevt class methods to convert these types into the original type. Both the XmlConvert and System.Convevt classes can implement data type conversion, but the XmlConvert class converts based on the data type specified in the XSD, regardless of what type it is now.

??Suppose you have the following XML data fragment:

??

??Let us first confirm that the birthdaydaay attribute value is February 8, 2001. If you use the System.Convert class to convert the string into .NET The DateTime type in the Framework, in this way, we can apply it as a date type. In contrast, if you use the XmlConvert class to convert a string, you will see a parsing error because the XmlConvert class cannot accurately interpret the date in the string. Because in XML, the format of date data must be in the form of YYYY-MM-DD. The XmlConvert class is responsible for the mutual conversion between CLR types and XSD types. When conversion work occurs, the conversion results are partial.

??In some solutions, the attribute value is composed of plain text and entities. Among all browser classes, only the XmlValidatingReader class can handle entities. Although XmlTextReader cannot handle entities, it can only take out the text values ​​when they appear in attribute values ​​at the same time. In this case, you must use the ReadAttributeValue method instead of a simple read method to analyze the content of the attribute value.

??The ReadAttributeValue method analyzes the attribute value, and then separates each component element (such as separating plain text and entities). You can use the return value of the ReadAttributeValue method as a loop condition to traverse the elements in all attribute values. Since the XmlTextReader class cannot handle entities, you can write a class for handling entities 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;

??}

??When all attribute values ​​are analyzed, the ReadAtributeValue method returns False, thus ending the loop. The ultimate result of the attribute value is the value of the global variable buffer.

??Processing XML text (Text)

??When we process XML tag text, if we cannot process it accurately, the cause of the error can be quickly determined. For example, if a character conversion error occurs, it must be transmitting 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 safely used as element and attribute names.

??The XmlConvert class provides the function of converting non-XML standard names into standard XML names. When tag names contain invalid XML characters, the EncodeName and DecodeName methods can adjust them into Schema-compliant XML names. Including 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. A typical situation is when you are dealing with column names in the database that contain spaces. Although SQL Server allows long column names, this may not be a valid name for the XML stream. Spaces will be replaced by the hexadecimal code Invoice_0x0020_Details. The following code demonstrates how to obtain the string in the program:

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

??The opposite method is DecodeName. This method converts the XML text into its original format. It should be noted 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');

? ?Whitespace in an XML document is either important or unimportant. It is important because it can express actual meaning when it is presented in the content of an element or when it is in a comment statement. For example, the following situation:



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

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

??•••

??< /MyNode>

?? In xml, spaces not only represent spaces (vacancies), but also carriage returns, line feeds and indents.

??You can handle spaces through the WhiteSpaceHandling property of the XmlTextReader class. This property receives and returns a WhiteSpaceHandling enumeration value (this enumeration class has three optional values). The default value is All, which means that both meaningful and meaningless spaces will be returned as nodes - distinguished as SignificantWhitespace and Whitespace nodes. Another enumeration value is None, which means that any spaces will not be returned as nodes. Finally, there is the Signficant enumeration value, which ignores meaningless spaces and only returns nodes with the node type SignficantWhitespace. Note that the WhiteSpaceHandling property is one of a few browser properties. It can be changed at any time and affect the Read operation. The Normalization and XmlResolver attributes are "Sensitive".

The above is the content of easily processing XML data (2-1) in .NET Framework. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template