[英]How to parse xs:annotation from the xs:choice using the System.Xml.Schema
我正在尝试在 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
的 Annotation 始终为 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 中,选择元素注释为空 :( 。有人可以提供一些工作示例或添加一些提示吗?任何帮助将不胜感激。
注释肯定可以放在xs:choice中。 查看从内联注释模式中获取的以下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中如下所示:
更新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之外作为基于你的实验的顶级元素]并解决你的第二个问题:[我有类似的经验从你的! 它将注释显示为null,尽管它不是]
对于遇到此问题的其他人,我通过反映System.Xml.Schema命名空间的类来找到,在编译模式集时,元素的注释被复制到它们的子节点。
所以Vytas999应该能够(我已经能够)通过检查XmlSchemaChoice.Items属性中的XmlSchemaParticle对象来找到他缺少的注释。
我遇到了同样的问题 - 我需要处理由外部实体提供的 XSD 并且XmlSchemaChoice
的Annotation
始终为空。
详细说明@Adrian 的答案,我成功使用的代码如下。 在我的例子中,我从根向下遍历模式,当我点击XmlSchemaSequence
我遍历它的 Items 集合:
var index = 0;
foreach (var childObject in sequence.Items)
{
ExtractElement(childObject, index);
++index ;
}
当 childObject 是XmlSchemaChoice
类型时(假设它在变量xmlSchemaChoice
,那么代替
// DOES NOT WORK - always null
var choiceAnnotation = xmlSchemaChoice.Annotation
我像这样访问注释:
((xmlSchemaChoice.Parent as XmlSchemaSequence)?.Items[index] as XmlSchemaChoice)?.Annotation
你会期待同样的结果吗? 嗯,这不是..
此代码只是描述通过父项访问注释的示例片段,它不是通用代码,可能需要对您的具体情况进行一些调整。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.