co********@yahoo.com wrote:
I am writing code with C# to generate xml schema, however, the
following code generate a UTF-16 in the first line.
public XmlSchema CreateSchemaRoot()
{
schema.ElementFormDefault = XmlSchemaForm.None;
schema.AttributeFormDefault = XmlSchemaForm.Unqualified;
schema.Version = "1.0";
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("xsd", schemaNS);
schema.Namespaces = ns;
return schema;
}
I don't see any statement in that code snippet that serializes the schema.
The following is a complete code snippet creating an example schema and
serializing it using a StreamWriter which by default uses UTF-8 to
encoded data:
XmlSchema exampleSchema = new XmlSchema();
XmlSchemaElement elementGod = new XmlSchemaElement();
elementGod.Name = "God";
elementGod.SchemaTypeName = new XmlQualifiedName("string",
XmlSchema.Namespace);
exampleSchema.Items.Add(elementGod);
exampleSchema.Compile(new ValidationEventHandler(ValidationHandler));
XmlNamespaceManager namespaceManager = new XmlNamespaceManager(new
NameTable());
namespaceManager.AddNamespace("xs", XmlSchema.Namespace);
using (StreamWriter schemaWriter = new
StreamWriter(@"test2005102601Xsd.xml")) {
exampleSchema.Write(schemaWriter, namespaceManager);
}
so that the resulting schema file looks as follows:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="God" type="xs:string" />
</xs:schema>
So whatever you are trying, I don't think there is anything to change in
the code you presented, you simply need to make sure to serialize the
schema in a way such that UTF-8 is used as the encoding. My sample shows
one way with the StreamWriter.
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/