co********@yaho o.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 CreateSchemaRoo t()
{
schema.ElementF ormDefault = XmlSchemaForm.N one;
schema.Attribut eFormDefault = XmlSchemaForm.U nqualified;
schema.Version = "1.0";
XmlSerializerNa mespaces ns = new XmlSerializerNa mespaces();
ns.Add("xsd", schemaNS);
schema.Namespac es = 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();
XmlSchemaElemen t elementGod = new XmlSchemaElemen t();
elementGod.Name = "God";
elementGod.Sche maTypeName = new XmlQualifiedNam e("string",
XmlSchema.Names pace);
exampleSchema.I tems.Add(elemen tGod);
exampleSchema.C ompile(new ValidationEvent Handler(Validat ionHandler));
XmlNamespaceMan ager namespaceManage r = new XmlNamespaceMan ager(new
NameTable());
namespaceManage r.AddNamespace( "xs", XmlSchema.Names pace);
using (StreamWriter schemaWriter = new
StreamWriter(@" test2005102601X sd.xml")) {
exampleSchema.W rite(schemaWrit er, namespaceManage r);
}
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/