添加“xs: Enumeration"XSD使用c#
本文关键字:XSD 使用 quot xs Enumeration 添加 | 更新日期: 2023-09-27 18:06:05
如何使用c#添加"xsi:Enumeration" XSD
这是基本文件XSD:
<xs:schema xmlns="urn:bookstore-schema"
targetNamespace="urn:bookstore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="TagType">
<xs:restriction base="xs:string">
<!--here to add new Enum elements -->
</xs:restriction>
</xs:simpleType>
和我想得到这个结果使用c#:
<xs:schema xmlns="urn:bookstore-schema"
targetNamespace="urn:bookstore-schema"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:simpleType name="TagType">
<xs:restriction base="xs:string">
<xs:enumeration value="Actor" />
<xs:enumeration value="Productor" />
<xs:enumeration value="Director" />
<xs:enumeration value="Category" />
</xs:restriction>
</xs:simpleType>
谢谢:)
可以使用System.xml.schema类编辑xsd文件。详细的例子可以在这个支持链接上找到。
http://support.microsoft.com/kb/318502/en-us下面是修改后的代码,用于向文件中添加两个枚举并重新保存文件。
FileStream fs;
XmlSchema schema;
ValidationEventHandler eventHandler =
new ValidationEventHandler(Class1.ShowCompileErrors);
try
{
fs = new FileStream("book.xsd", FileMode.Open);
schema = XmlSchema.Read(fs, eventHandler);
schema.Compile(eventHandler);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(schema);
schemaSet.Compile();
schema = schemaSet.Schemas().Cast<XmlSchema>().First();
var simpleTypes =
schema.SchemaTypes.Values
.OfType<XmlSchemaSimpleType>()
.Where(t => t.Content is XmlSchemaSimpleTypeRestriction);
foreach (var simpleType in simpleTypes)
{
XmlSchemaSimpleTypeRestriction restriction =
(XmlSchemaSimpleTypeRestriction)simpleType.Content;
XmlSchemaEnumerationFacet actorEnum =
new XmlSchemaEnumerationFacet();
actorEnum.Value= "Actor";
restriction.Facets.Add(actorEnum);
XmlSchemaEnumerationFacet producerEnum =
new XmlSchemaEnumerationFacet();
producerEnum.Value = "Producer";
restriction.Facets.Add(producerEnum);
}
fs.Close();
fs = new FileStream("book.xsd", FileMode.Create);
schema.Write(fs);
fs.Flush();
fs.Close();
}
catch (XmlSchemaException schemaEx)
{
Console.WriteLine(schemaEx.Message);
}
catch (XmlException xmlEx)
{
Console.WriteLine(xmlEx.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
Console.Read();
}