Easily handle XML data in .NET Framework (2)

zhaozj2021-02-17  60

Analysis attribute value

In most cases, the attribute value is a simple text string. However, this does not mean that attribute values ​​in practical applications are characters. Sometimes, attribute values ​​are combined by many types of data, such as Date or Boolean, at this time, you have to convert these types into the original type with XMLConvert or System.convevt classes. Both XMLConvert and System.convevt classes enable conversion of data types, but the XMLConvert class converts the data type specified in XSD, regardless of what type it is now.

Suppose you have the following XML data pieces:

Let us first confirm that the BIRTHDAY attribute value is February 8, 2001. If you convert the string into the DateTime type in the .NET Framework in the system.convert class, we can use it as a Date type. Compared to the XMLConvert class to convert a string, you will see an analysis error because the XMLConvert class does not explain the date in this string. Because in XML, the format of date data must be in YYYY-MM-DD form. The XMLConvert class serves as the mutual conversion between the CLR type and the XSD type. When the conversion work occurs, the conversion result is partial.

In some solutions, the attribute value is constructed from plain text and entity. In all reader classes, only XMLValidatingReader class can handle entities. Although XMLTextReader cannot handle entities, they simultaneously appear in the attribute value, it can only take the text value. This happens, you must use the ReadttributeValue method to replace simple readings to analyze the contents of the property.

The READATTRIBUTEVALUE method analyzes the property value and then separates the elements of each component (such as separating plain text and entity). You can use the return value of the ReadAttributeValue method as a loop condition, traversing the elements of the entire property value. Since the XMLTextReader class cannot handle the entity, you can write a class that is used to handle the entity. 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 the attribute value is all 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.

Handling XML text (Text)

When we do not process the XML tag text, it cannot be determined quickly. For example, a character conversion error is inevitably transmitted in an XML data stream. Not all valid characters in a given platform are valid XML characters. Only valid characters specified in the XML specification (www.w3.rg/tr/2000/rec-xml-20001006.html) can be safely used as elements and attribute names.

The XMLConvert class provides features that convert non-XML standards to standard XML naming. Encodename and DecodeEname methods are adjusted into XML naming that complies with Schema when the label name contains invalid XML characters. Includes SQL ServerTM and Microsoft Office, which allows and supports Unicode documents, however, characters in these documents are not a valid XML naming. Typical situations are when you handle column names that contain spaces in your database. Although SQL Server allows the long name, this may not be a valid naming for XML flow. The space is replaced by hexadecimal code invoice_0x0020_details. The following code demonstrates how to get the string in the program: XMLConvert.Encodename ("Invoice Details");

The opposite method is Decodename. This method converts XML text into its original format. It should be noted that it can only convert a complete hexadecimal code, only _0x0020_ is only used as a space, and _0x20_ is not:

XmlConvert.Decodename ("Invoice_0x0020_details);

The space in the XML document is not important. Say it is important, it is the actual meaning when it appears in the content of the element or it is in the comment statement. For example, the following situation:

•••••

In XML, spaces are not just represents spaces (blank), and they represent them on their own business.

You can handle spaces through the WhitespaceHandling property of the XMLTextReader class. This attribute accepts and returns a WhitespaceHandling enumeration value (this enumeration class has three optional values). The default is all, which indicates that the meaningful and meaningless space will return as a node ---- SignificantWhitespace and Whitespace nodes, respectively. Another enumeration value is none, which means that it is not returned to any space. Finally, the SignFICANT enumeration value, which means that ignores the meaningless space, and only returns the node of the node type as SignficantWhitespace. Note whitespaceHandling property is one of a few reader properties. It can be changed at any time and giving the read operation. The Normalization and XMLResolver properties are "Sensitive".

String and fragment

The programmer cuts the program in MSXML and discovers the difference between COM and .NET Framework XML APIs. The .NET Framework class itself does not provide a method to analyze XML data stored in a string. Unlike MSXML analyzer objects, the XMLTestReader class does not provide any LoadXML method to create a reader from a wellform in a format. No way to provide logadxml because you can get the same feature with a special Text Reader --- StringReader class.

XMLTextReader is one of the constructor accepts a TextReader derived object and an XML reader parameter (creating the reader based on the contents of Text Reader). A TEXT Reader class is a stream that is an input character to be generated. The StringReader class inherits the TextReader class and uses a string in memory as its input stream. The following code snippet demonstrates how to initialize an XML Reader, use a good XML string as its input:

String XmlText = "...";

StringReader strreader = new stringReader (XmlText); XmlTextReader Reader = New XMLTextReader (STREADER);

In addition, use the StringWriter class instead of the TextWrite class, you can create an XML document from the memory characters.

A specified type of XML string is an XML fragment. The XML piece consists of XML text, but the XML document without a root node is not a well-format XML document, so it cannot be applied. An XML piece is part of the original document, so it may miss the root node. For example, the following XML text is a valid XML piece, but it is not a valid XML document because it has no root node:

Dino

esposito

The .NET Framework XML API allows programmers to use the XML fragment with one analyzer content, the analyzer content consists of an Encoding character set, DTD document, namespace, language, and space handler:

Public XMLTextReader

String XMLFRAGMENT,

XMLNodetype FragType,

XMLParserContext Context

);

XMLFragment

Parameters include

Xml

String analysis.

FRAGTYPE

Parameter representation

Fragment

Type, it gives it

Fragment

Type of root node. only

ELEMENT, ATTIBUTE

with

Document

Type node can be used as

Fragment

The root node, the content of the analyzer can be

XMLParserContext

Class interpretation.

转载请注明原文地址:https://www.9cbs.com/read-29322.html

New Post(0)