Microsoft KB Archive/815109

From BetaArchive Wiki

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.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Microsoft Visual C# .NET or Microsoft Visual Basic .NET. Under Templates, click Console Application.
  4. Name the project MyTestConsoleApplication, and then click OK.
  5. 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(@"..\..\Test.xsd");
                XmlSchema myXmlSchema = XmlSchema.Read(reader, null);
                reader.Close();
    
                // Write the XML Schema file to the Console.
                myXmlSchema.Write(Console.Out);
                Console.Write("\n\n\n\nPress ENTER to quit .....");
                Console.Read();
                Console.WriteLine("");
             }
             catch (Exception e) 
             {
                // Catch exceptions, if any exist.
                Console.WriteLine(e);
                Console.Write("\n\n\n\nPress ENTER to quit .....");
                Console.Read();
                Console.WriteLine("");
             }
          }
       }
    }

    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("..\Test.xsd")
             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("")
             Console.Write("Press ENTER to quit .....")
             Console.Read()
             Console.WriteLine("")
          Catch e As Exception
             Console.WriteLine(e)
             Console.Write("Press ENTER to quit .....")
             Console.Read()
             Console.WriteLine("")
          End Try
       End Sub
    End Module
  6. In Solution Explorer, right-click MyTestConsoleApplication, click Add, and then click Add New Item.
  7. Under Categories, click Local Project Items. Under Templates, click XML Schema.
  8. Name the schema file Test.xsd, and then click Open.
  9. Right-click the Test.xsd page, and then click View XML Source.
  10. Replace the existing XML Schema code with the following code:

    <?xml version="1.0" encoding="utf-8" ?>
    <xs:schema xmlns:mstns="http://tempuri.org/Dataset1.xsd" 
    xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" 
    xmlns:NameSpace1="http://tempuri.org/XMLSchema1.xsd" 
    xmlns="http://tempuri.org/Dataset1.xsd" attributeFormDefault="qualified" 
    elementFormDefault="qualified" targetNamespace="http://tempuri.org/Dataset1.xsd" 
    id="Dataset1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:import namespace="http://tempuri.org/XMLSchema1.xsd" />
       <xs:element msdata:IsDataSet="true" name="Dataset1">
          <xs:complexType>
             <xs:choice maxOccurs="unbounded">
                <xs:element name="Table" type="NameSpace1:Famous_x0020_People" />
             </xs:choice>
          </xs:complexType>
       </xs:element>
    </xs:schema>
    <xs:schema xmlns="http://tempuri.org/XMLSchema1.xsd" elementFormDefault="qualified" 
    targetNamespace="http://tempuri.org/XMLSchema1.xsd" id="XMLSchema1" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema">
       <xs:complexType name="Famous_x0020_People">
          <xs:sequence>
             <xs:element minOccurs="0" name="Name" type="xs:string" />
             <xs:element minOccurs="0" name="Role" type="xs:string" />
             <xs:element minOccurs="0" name="BirthDate" type="xs:date" />
          </xs:sequence>
       </xs:complexType>
    </xs:schema>
  11. On the Debug menu, click Start.

    In the Console output, you may find

     <xs:element name="Table" type="NameSpace1:Famous_x005F_x0020_People" />

    instead of

    <xs:element name="Table" type="NameSpace1:Famous_x0020_People" />


REFERENCES

For additional information about XML Schemas in the Microsoft .NET Framework, click the following article number to view the article in the Microsoft Knowledge Base:

313826 Roadmap for XML schemas in the .NET Framework


Keywords: kbfilesystems kbschema kbxml kbprb kbfileio KB815109