从 XSD 文件中提取枚举值?
Extract enumeration values from XSD file?
我正在尝试从 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 文档的名称和描述,例如"Bag, super bulk"等
虽然没有那么优雅,但我最终还是使用了旧的 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);
}
如果有人有更好的方法,请分享。
我正在尝试从 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 文档的名称和描述,例如"Bag, super bulk"等
虽然没有那么优雅,但我最终还是使用了旧的 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);
}
如果有人有更好的方法,请分享。