Invalid character handling in. NET XML

Source: Internet
Author: User
Tags locale setting microsoft sql server net xml

<summary>
Replace partially invalid characters into null
</summary>
XML data returned after <param name= "responsexml" &GT;GTA server response </param>
<returns>string</returns>
private string Converthex (String responsexml)
{
UTF8Encoding encoding = new UTF8Encoding ();
byte[] Responsebytearr = encoding. GetBytes (Responsexml);
for (int i = 0; i < responsebytearr.length; i++)
{
if (Responsebytearr[i] < 32)
{
Responsebytearr[i] = 32;
}
}
return encoding. GetString (Responsebytearr);
}

=======================================

Convertxml is primarily used for XML names and the use of the conversion phase magnetic type with XSD

The following is from MSDN

Encodes and decodes XML names and provides methods for converting between common language type library types and XML Schema definition language (XSD) types. When you convert a data type, the value returned is independent of the locale setting.

The element and attribute names or ID values are limited to a series of XML characters, as recommended by the Extensible Markup Language (XML) 1.0 (second Edition) located in Www.w3.org/TR/2000/REC-xml-20001006.html. When names contain invalid characters, theencodename and decodename methods are used to convert them to valid XML names.

Many languages and applications, such as Microsoft SQL Server and Microsoft Word, allow Unicode characters to be used in their names, which are not valid in XML names. For example, if order Detail is a column header in a database, the database allows spaces between the word order and the Detail. However, in XML, the space between the order and the Detail is treated as an invalid XML character. In this way, the space (an invalid character) needs to be converted to an escaped hexadecimal encoding, and the encoding can be decoded later.

You can use the encodename method and XmlWriter class to make sure that the name you are writing is a valid XML name. The following C # code converts the name "Order Detail" to a valid XML name and writes the element <order_0x0020_detail>my order</order_0x0020_detail>.

Copy Code
Writer. WriteElementString (Xmlconvert.encodename ("Order Detail"), "the", "my";

XmlConvert also provides methods that enable you to convert between strings and. NET Framework data types. Zone settings are not considered during data conversion. The data type is based on an XML schema (XSD) data type.

notice

In the. NET Framework version 2.0, The XmlReader, XmlWriter , and XPathNavigator classes contain new methods that can handle typed data. These new methods handle the conversion of the string to the. NET Framework data type for you.

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.