Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 加上「;xs:枚举“;使用C的XSD#_C#_Xml_Enums_Xsd - Fatal编程技术网

C# 加上「;xs:枚举“;使用C的XSD#

C# 加上「;xs:枚举“;使用C的XSD#,c#,xml,enums,xsd,C#,Xml,Enums,Xsd,如何使用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:s

如何使用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#得到这个结果:



谢谢:)

您可以使用System.xml.schema类编辑xsd文件。 详细示例可在此支持链接上找到

下面是用于向文件中添加两个枚举并重新保存文件的修改代码

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();
}
filestreamfs;
XmlSchema;
ValidationEventHandler事件处理程序=
新的ValidationEventHandler(Class1.ShowCompileErrors);
尝试
{
fs=newfilestream(“book.xsd”,FileMode.Open);
schema=XmlSchema.Read(fs,eventHandler);
Compile(eventHandler);
XmlSchemaSet schemaSet=新的XmlSchemaSet();
schemaSet.Add(schema);
schemaSet.Compile();
schema=schemaSet.Schemas().Cast().First();
单纯形变量=
schema.SchemaTypes.Values
第()类
其中(t=>t.Content是XmlSchemaSimpleTypeRestriction);
foreach(simpleType中的var simpleType)
{
XmlSchemaSimpleTypeRestriction限制=
(XmlSchemaSimpleTypeRestriction)simpleType.Content;
XmlSchemaEnumerationFacet actorEnum=
新的XmlSchemaEnumerationFacet();
actorEnum.Value=“Actor”;
限制.Facets.Add(actorEnum);
XmlSchemaEnumerationFacet producerEnum=
新的XmlSchemaEnumerationFacet();
producerEnum.Value=“生产者”;
限制。刻面。添加(producerEnum);
}
fs.Close();
fs=newfilestream(“book.xsd”,FileMode.Create);
schema.Write(fs);
fs.Flush();
fs.Close();
}
捕获(XmlSchemaException schemaEx)
{
Console.WriteLine(schemaEx.Message);
}
捕获(XmlException xmlEx)
{
Console.WriteLine(xmlEx.Message);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
最后
{
Console.Read();
}

您可以使用System.xml.schema类编辑xsd文件。 详细示例可在此支持链接上找到

下面是用于向文件中添加两个枚举并重新保存文件的修改代码

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();
}
filestreamfs;
XmlSchema;
ValidationEventHandler事件处理程序=
新的ValidationEventHandler(Class1.ShowCompileErrors);
尝试
{
fs=newfilestream(“book.xsd”,FileMode.Open);
schema=XmlSchema.Read(fs,eventHandler);
Compile(eventHandler);
XmlSchemaSet schemaSet=新的XmlSchemaSet();
schemaSet.Add(schema);
schemaSet.Compile();
schema=schemaSet.Schemas().Cast().First();
单纯形变量=
schema.SchemaTypes.Values
第()类
其中(t=>t.Content是XmlSchemaSimpleTypeRestriction);
foreach(simpleType中的var simpleType)
{
XmlSchemaSimpleTypeRestriction限制=
(XmlSchemaSimpleTypeRestriction)simpleType.Content;
XmlSchemaEnumerationFacet actorEnum=
新的XmlSchemaEnumerationFacet();
actorEnum.Value=“Actor”;
限制.Facets.Add(actorEnum);
XmlSchemaEnumerationFacet producerEnum=
新的XmlSchemaEnumerationFacet();
producerEnum.Value=“生产者”;
限制。刻面。添加(producerEnum);
}
fs.Close();
fs=newfilestream(“book.xsd”,FileMode.Create);
schema.Write(fs);
fs.Flush();
fs.Close();
}
捕获(XmlSchemaException schemaEx)
{
Console.WriteLine(schemaEx.Message);
}
捕获(XmlException xmlEx)
{
Console.WriteLine(xmlEx.Message);
}
捕获(例外情况除外)
{
控制台写入线(例如消息);
}
最后
{
Console.Read();
}

谢谢您的回答,但是,我已经使用了这个示例,但是我没有找到如何添加Ok。。所以我现在添加了源代码,通过它可以添加枚举。希望有帮助!谢谢你的回答,但是,我还用过这个例子,但是我不知道如何添加Ok。。所以我现在添加了源代码,通过它可以添加枚举。希望有帮助!