如何使用System.Xml.Schema从xs:choice解析xs:annotation

时间:2014-09-19 15:37:35

标签: c# xml xml-parsing xsd choice

我试图在xs:choice中添加注释元素。根据xs:choice语法,这是可能的。我在BTW里找不到带注释的选择样本。我当前版本的xsd文件包含一个元素:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.es.de/es3/flex/simple"
             elementFormDefault="qualified"
             xmlns="http://www.es.de/es3/flex/simple"
             xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:flex="http://www.es.de/es3/flex/flexBase">

    <xs:import namespace="http://www.es.de/es3/flex/flexBase" />

    <xs:element name="ESS3754">
        <xs:complexType>
            <xs:choice>
                <xs:annotation>
                    <xs:appinfo>
                        <flex:ControlHeadline>Headline_VVVVV</flex:ControlHeadline>
                        <flex:helpText>HelpText_VVVVV</flex:helpText>
                    </xs:appinfo>
                </xs:annotation>
                <xs:element name="String1" type="xs:string" minOccurs="1" maxOccurs="1"/>
            </xs:choice>
        </xs:complexType>
    </xs:element>

</xs:schema>

但是,在解析xsd文件时,对象System.Xml.Schema.XmlSchemaChoice的注释始终为null。

代码部分:

public List<FSBaseItem> Parse( XmlTextReader xsdReader )
        {
            try
            {
                // prepare schema set for schema validation and raw template xsd "enrichment"
                XmlSchemaSet schemaSet = new XmlSchemaSet();
                schemaSet.ValidationEventHandler += ValidationCallbackOne;

                // include base schema
                XmlSchema baseXsd = FlexXmlSchemaReader.ReadBase();
                schemaSet.Add( baseXsd );

                // The Read method will throw errors encountered on parsing the schema
                XmlSchema xsd = XmlSchema.Read( xsdReader, ValidationCallbackOne );
                schemaSet.Add( xsd );

                // The Compile method will throw errors encountered on compiling the schema
                schemaSet.Compile();

                // create root
                FSElement rootElement = new FSElement( this.GetNewId() );
                // traverse body
                this.TraverseSOM( xsd, rootElement );
                // validate
                this.ValidateFSItems( rootElement.Items );
                // init lists containers with minimum elements
                InitEmptyFEListItems( rootElement );                

                return rootElement.Items;
            }
            finally
            {
                xsdReader.Close();
            }
        }

已经在beginig中,choice元素注释为null :(。有人可以提供一些工作样本或添加一些提示吗?任何帮助都会受到赞赏。

2 个答案:

答案 0 :(得分:4)

注释当然可以放在xs:choice中。查看从Inline Annotated Schema

获取的以下xsd
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" 
  xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" 
  jaxb:version="1.0" jaxb:extensionBindingPrefixes="xjc">
    <xs:annotation>
        <xs:appinfo>
            <jaxb:globalBindings>
                <xjc:superClass name="com.syh.Shape"/>
            </jaxb:globalBindings>
        </xs:appinfo>
    </xs:annotation>
    <xs:element name="Widgets">
        <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
                <xs:annotation>
                    <xs:appinfo>
                        <jaxb:property name="Shapes"/>
                    </xs:appinfo>
                </xs:annotation>
                <xs:element name="Rectangle" type="Rectangle"/>
                <xs:element name="Square" type="Square"/>
                <xs:element name="Circle" type="Circle"/>
            </xs:choice>
         </xs:complexType>
    </xs:element>
    <xs:complexType name="Rectangle">
        <xs:sequence>
            <xs:element name="Width" type="xs:integer"/>
            <xs:element name="Height" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Square">
        <xs:sequence>
            <xs:element name="Length" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="Circle">
        <xs:sequence>
            <xs:element name="Radius" type="xs:integer"/>
        </xs:sequence>
    </xs:complexType>
</xs:schema>

使类似的策略适应xsd会产生以下内容:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.es.de/es3/flex/simple"
             elementFormDefault="qualified"
             xmlns="http://www.es.de/es3/flex/simple"
             xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:flex="http://www.es.de/es3/flex/flexBase">

  <xs:import namespace="http://www.es.de/es3/flex/flexBase" />

  <xs:element name="ESS3754">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:annotation>
          <xs:appinfo>
            <flex:ControlHeadline>Headline_VVVVV</flex:ControlHeadline>
            <flex:helpText>HelpText_VVVVV</flex:helpText>
          </xs:appinfo>
        </xs:annotation>
        <xs:element name="String1" type="xs:string" minOccurs="1" maxOccurs="10"/>
        <xs:element name="NewlyAdded" type="Coordinate" minOccurs="1" maxOccurs="10"/>
      </xs:choice>
    </xs:complexType>    
  </xs:element>
  <xs:complexType name="Coordinate">
  <xs:sequence>
    <xs:element name="LocationX" type="xs:integer"/>
    <xs:element name="LocationY" type="xs:integer"/>
    <xs:element name="LocationZ" type="xs:integer"/>
  </xs:sequence>
</xs:complexType>    

</xs:schema>

并且xsd完全有效,在Visual Studio [XSD] Desginer中如下所示:

enter image description here

更新1

我同意调试器将项目注释显示为null [我找不到但我应该找到它]并且这非常令人沮丧。我使用代码重新构建了文档,您可以使用以下解决方法来注释元素: 考虑以下XSD,它没有任何XmlSchemaChoice并保存为stack-problem2.xsd

<?xml version="1.0" encoding="utf-8"?>
<xs:schema targetNamespace="http://www.es.de/es3/flex/simple"
             elementFormDefault="qualified"
             xmlns="http://www.es.de/es3/flex/simple"
             xmlns:mstns="http://tempuri.org/XMLSchema.xsd"
             xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:flex="http://www.es.de/es3/flex/flexBase">

  <xs:import namespace="http://www.es.de/es3/flex/flexBase" />




  <xs:complexType name="Coordinate">
    <xs:sequence>
      <xs:element name="LocationX" type="xs:integer"/>
      <xs:element name="LocationY" type="xs:integer"/>
      <xs:element name="LocationZ" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>  
</xs:schema>

现在您可以将其加载到内存中并以编程方式向XmlSchemaChoice元素添加注释:

public void Parse()
{
    try
    {
        XmlTextReader reader2 = new XmlTextReader(@"stack-problem2.xsd");
        XmlSchema myschema2 = XmlSchema.Read(reader2, ValidationCallback);


        var simpleAnotation = new XmlSchemaAnnotation();
        simpleAnotation.Id = "Lost Anotation";

        // <xs:complexType name="ESS3754">
        XmlSchemaComplexType complexType = new XmlSchemaComplexType();
        myschema2.Items.Add(complexType);
        complexType.Name = "ESS3754";

        // <xs:choice minOccurs="1" maxOccurs="1">
        XmlSchemaChoice choice = new XmlSchemaChoice();
        complexType.Particle = choice;
        choice.MinOccurs = 1;
        choice.MaxOccurs = 1;

        XmlSchemaElement elementSelected = new XmlSchemaElement();
        choice.Items.Add(elementSelected);
        elementSelected.Name = "String1";

        AnnonateMyComplexType(choice); 

        FileStream file = new FileStream(@"satck-solution.xsd", FileMode.Create, FileAccess.ReadWrite);
        XmlTextWriter xwriter = new XmlTextWriter(file, new UTF8Encoding());
        xwriter.Formatting = Formatting.Indented;
        myschema2.Write(xwriter);
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
}

public static void AnnonateMyComplexType(XmlSchemaChoice xmlSchemaComplexType)
{

    XmlSchemaAnnotation myCustomAnnotation = new XmlSchemaAnnotation();
    xmlSchemaComplexType.Annotation = myCustomAnnotation;

    // <xs:documentation>State Name</xs:documentation>
    XmlSchemaDocumentation schemaDocumentation = new XmlSchemaDocumentation();
    myCustomAnnotation.Items.Add(schemaDocumentation);
    schemaDocumentation.Markup = TextToNodeArray("Headline_VVVVV");

    // <xs:appInfo>Application Information</xs:appInfo>
    XmlSchemaAppInfo appInfo = new XmlSchemaAppInfo();
    myCustomAnnotation.Items.Add(appInfo);
    appInfo.Markup = TextToNodeArray("Headline_VVVVV");

}

static void ValidationCallback(object sender, ValidationEventArgs args)
{
    if (args.Severity == XmlSeverityType.Warning)
        Console.Write("WARNING: ");
    else if (args.Severity == XmlSeverityType.Error)
        Console.Write("ERROR: ");

    Console.WriteLine(args.Message);
}

在上面运行将返回以下XSD文件:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://www.es.de/es3/flex/simple" xmlns:flex="http://www.es.de/es3/flex/flexBase" xmlns:mstns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" targetNamespace="http://www.es.de/es3/flex/simple" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.es.de/es3/flex/flexBase" />
  <xs:complexType name="Coordinate">
    <xs:sequence>
      <xs:element name="LocationX" type="xs:integer" />
      <xs:element name="LocationY" type="xs:integer" />
      <xs:element name="LocationZ" type="xs:integer" />
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ESS3754">
    <xs:choice minOccurs="1" maxOccurs="1">
      <xs:annotation>
        <xs:documentation>Headline_VVVVV</xs:documentation>
        <xs:appinfo>Headline_VVVVV</xs:appinfo>
      </xs:annotation>
      <xs:element name="String1" />
    </xs:choice>
  </xs:complexType>
</xs:schema>

所以回答你的第一个问题:是的,注释可以放在XmlSchemaChoice元素中(通过代码和直接)[不一定在xmlSchemaChoice之外作为基于你实验的顶级元素]并解决你的第二个问题:[I有类似的经验!它将注释显示为null,尽管它不是]

答案 1 :(得分:0)

对于遇到此问题的其他人,我通过反映System.Xml.Schema命名空间的类来找到,在编译模式集时,元素的注释被复制到它们的子节点。

所以Vytas999应该能够(我已经能够)通过检查XmlSchemaChoice.Items属性中的XmlSchemaParticle对象找到他缺少的注释。