Microsoft KB Archive/815109

= Previously encoded characters in an XML Schema file are encoded again when you write the XML Schema to a new file =

Article ID: 815109

Article Last Modified on 2/5/2004

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition

-





Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.

No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location where you downloaded the release.



SYMPTOMS
When you use the XmlSchema class to read an XML Schema (.xsd) file that includes an encoded character such as a space character, and then you use the Write method to write the XML Schema to another XML Schema file, you may notice that the encoded characters have been encoded again in the new XML Schema file.



CAUSE
The XMLSchema class uses the XmlSerializer class to encode the data before writing the data to another XML Schema file. Therefore, if the XmlSchema object already contains encoded data, the Write method encodes the data again, and then it writes the data to the file.



STATUS
This behavior is by design.



Steps to reproduce the behavior
 Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Microsoft Visual C# .NET or Microsoft Visual Basic .NET. Under Templates, click Console Application. Name the project MyTestConsoleApplication, and then click OK.  Replace the existing code with the following code:

Visual C# .NET Code using System; using System.IO; using System.Xml; using System.Xml.Schema;

namespace MyNameSpace {  public class MyClass {     // This method (Main) reads the XML file by using the XmlSchema class // and then writes the XML Schema on the Console. public static void Main(string []args) {        try {           // Read the XML Schema file by using the XmlTextReader class. XmlTextReader reader = new XmlTextReader(@&quot;..\..\Test.xsd&quot;); XmlSchema myXmlSchema = XmlSchema.Read(reader, null); reader.Close;

// Write the XML Schema file to the Console. myXmlSchema.Write(Console.Out); Console.Write(&quot;\n\n\n\nPress ENTER to quit .....&quot;); Console.Read; Console.WriteLine(&quot;&quot;); }        catch (Exception e)          { // Catch exceptions, if any exist. Console.WriteLine(e); Console.Write(&quot;\n\n\n\nPress ENTER to quit .....&quot;); Console.Read; Console.WriteLine(&quot;&quot;); }     }   } }  Visual Basic .NET Code Imports System Imports System.IO Imports System.Xml Imports System.Xml.Schema

Module Module1 'This method (Main) reads the XML file by using the XmlSchema class 'and then writes the XML Schema on the Console. Sub Main Try ' Read the XML Schema file by using the XmlTextReader class. Dim reader As New XmlTextReader(&quot;..\Test.xsd&quot;) Dim myXmlSchema As XmlSchema myXmlSchema = myXmlSchema.Read(reader, Nothing) reader.Close

' Write the XML Schema file to the Console. myXmlSchema.Write(Console.Out) Console.WriteLine(&quot;&quot;) Console.Write(&quot;Press ENTER to quit .....&quot;) Console.Read Console.WriteLine(&quot;&quot;) Catch e As Exception Console.WriteLine(e) Console.Write(&quot;Press ENTER to quit .....&quot;) Console.Read Console.WriteLine(&quot;&quot;) End Try End Sub End Module </li> In Solution Explorer, right-click MyTestConsoleApplication, click Add, and then click Add New Item.</li> Under Categories, click Local Project Items. Under Templates, click XML Schema.</li> Name the schema file Test.xsd, and then click Open.</li> Right-click the Test.xsd page, and then click View XML Source.</li>  Replace the existing XML Schema code with the following code: <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot; ?> <xs:schema xmlns:mstns=&quot;http://tempuri.org/Dataset1.xsd&quot; xmlns:msdata=&quot;urn:schemas-microsoft-com:xml-msdata&quot; xmlns:NameSpace1=&quot;http://tempuri.org/XMLSchema1.xsd&quot; xmlns=&quot;http://tempuri.org/Dataset1.xsd&quot; attributeFormDefault=&quot;qualified&quot; elementFormDefault=&quot;qualified&quot; targetNamespace=&quot;http://tempuri.org/Dataset1.xsd&quot; id=&quot;Dataset1&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:import namespace=&quot;http://tempuri.org/XMLSchema1.xsd&quot; /> <xs:element msdata:IsDataSet=&quot;true&quot; name=&quot;Dataset1&quot;> <xs:complexType> <xs:choice maxOccurs=&quot;unbounded&quot;> <xs:element name=&quot;Table&quot; type=&quot;NameSpace1:Famous_x0020_People&quot; /> </xs:choice> </xs:complexType> </xs:element> </xs:schema> <xs:schema xmlns=&quot;http://tempuri.org/XMLSchema1.xsd&quot; elementFormDefault=&quot;qualified&quot; targetNamespace=&quot;http://tempuri.org/XMLSchema1.xsd&quot; id=&quot;XMLSchema1&quot; xmlns:xs=&quot;http://www.w3.org/2001/XMLSchema&quot;> <xs:complexType name=&quot;Famous_x0020_People&quot;> <xs:sequence> <xs:element minOccurs=&quot;0&quot; name=&quot;Name&quot; type=&quot;xs:string&quot; /> <xs:element minOccurs=&quot;0&quot; name=&quot;Role&quot; type=&quot;xs:string&quot; /> <xs:element minOccurs=&quot;0&quot; name=&quot;BirthDate&quot; type=&quot;xs:date&quot; /> </xs:sequence> </xs:complexType> </xs:schema> </li>  On the Debug menu, click Start.

In the Console output, you may find <xs:element name=&quot;Table&quot; type=&quot;NameSpace1:Famous_x005F_x0020_People&quot; /> instead of <xs:element name=&quot;Table&quot; type=&quot;NameSpace1:Famous_x0020_People&quot; /> </li></ol>

<div class="references_section">