我正在尝试从xsd文件中提取枚举值,但是失败了,下面唯一的辅助代码,只能让我得到数字值。
XSD文件:
<xsd:simpleType name="PackageTypeCodeContentType">
<xsd:restriction base="xsd:token">
<xsd:enumeration value="43">
<xsd:annotation>
<xsd:documentation xml:lang="en">
<ccts:Name>Bag, super bulk</ccts:Name>
</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="44">
<xsd:annotation>
<xsd:documentation xml:lang="en">
<ccts:Name>Bag, polybag</ccts:Name>
<ccts:Description>A type of plastic bag, typically used to wrap promotional pieces, publications, product samples, and/or catalogues.</ccts:Description>
</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>
<xsd:enumeration value="1A">
<xsd:annotation>
<xsd:documentation xml:lang="en">
<ccts:Name>Drum, steel</ccts:Name>
</xsd:documentation>
</xsd:annotation>
</xsd:enumeration>...
我尝试使用的代码:
XmlSchema schema = XmlSchema.Read(XmlReader.Create("XsdLookups\\PackageType.xsd"), ValidationCallbackOne);
XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add(schema);
schemaSet.Compile();
var results = schema.Items.OfType<XmlSchemaSimpleType>().Where(s => (s.Content is XmlSchemaSimpleTypeRestriction) && s.Name == "PackageTypeCodeContentType").ToList();
var enumValues = results.SelectMany(c => ((XmlSchemaSimpleTypeRestriction)c.Content).Facets.OfType<XmlSchemaEnumerationFacet>().Select(d => d.Value));
enumValues.ToList().ForEach(p=> Debug.WriteLine(p));
我正在尝试提取xsd文档的名称和说明,例如“袋子,超级散装”等
答案 0 :(得分:0)
虽然不那么优雅,但我还是使用了不错的旧版Xml.Linq:
XDocument schema = XDocument.Load(xsdPath);
XNamespace xs = schema.Root?.Name.Namespace;
var enumerations = schema
.Root?
.Descendants(xs + "simpleType")
.Where(t => (string) t.Attribute("name") == simpleTypeName)
.Descendants(xs + "enumeration")
.ToList();
foreach (var element in enumerations)
{
var annotation = element.Elements().ElementAt(0);
var documentationElement = annotation.Elements().ElementAt(0);
//
var nameValue = documentationElement.Elements().ElementAt(0).Value;
var decsValue = documentationElement.Elements().ElementAtOrDefault(1)?.Value;
addMethod.Invoke(nameValue, decsValue);
}
如果有更好的方法,请分享。