[英]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.