如何使用.net?
以编程方式从xsd模式文件中提取元素的枚举约束值 例如,我想从以下xsd中提取“奥迪”,“高尔夫”和“宝马”:<xs:element name="car">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:enumeration value="Audi"/>
<xs:enumeration value="Golf"/>
<xs:enumeration value="BMW"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
答案 0 :(得分:4)
有一个XmlSchema
课程,但看起来很漂亮......“很有趣”。
xml查询是否足够?
XmlDocument doc = new XmlDocument();
doc.Load("Foo.xsd");
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace("xx", "http://www.w3.org/2001/XMLSchema");
foreach (XmlElement el in doc.SelectNodes("//xx:element[@name='car'][1]/xx:simpleType/xx:restriction/xx:enumeration", mgr))
{
Console.WriteLine(el.GetAttribute("value"));
}
答案 1 :(得分:4)
您还可以遍历架构中的所有项目。
以下代码对于从XSD架构中提取数据非常有用。 基本上它调用SOM中永远对象的函数回调。 您需要为回调提供一个实现,该回调检查类型是枚举并相应地处理它。
#region CallForEveryElement
public delegate void SchemaObjectFunction(XmlSchemaObject xso);
public static void CallForEveryElement(XmlSchema schema, SchemaObjectFunction function)
{
CallForEveryElementInternal(schema, function);
}
private static void CallForEveryElementInternal(XmlSchemaObject schemaObject, SchemaObjectFunction function)
{
if (schemaObject == null)
return;
function(schemaObject);
if (schemaObject is XmlSchema)
{
CallForEveryElementInternal((schemaObject as XmlSchema).Includes, function);
CallForEveryElementInternal((schemaObject as XmlSchema).Items, function);
}
if (schemaObject is XmlSchemaGroupBase)
{
CallForEveryElementInternal((schemaObject as XmlSchemaGroupBase).Items, function);
}
if (schemaObject is XmlSchemaAttribute)
{
CallForEveryElementInternal((schemaObject as XmlSchemaAttribute).SchemaType, function);
}
if (schemaObject is XmlSchemaAttributeGroup)
{
CallForEveryElementInternal((schemaObject as XmlSchemaAttributeGroup).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaAttributeGroup).Attributes, function);
}
if (schemaObject is XmlSchemaContentModel)
{
CallForEveryElementInternal((schemaObject as XmlSchemaContentModel).Content, function);
}
if (schemaObject is XmlSchemaComplexContentExtension)
{
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).Attributes, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentExtension).Particle, function);
}
if (schemaObject is XmlSchemaComplexContentRestriction)
{
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).Attributes, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexContentRestriction).Particle, function);
}
if (schemaObject is XmlSchemaComplexType)
{
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).Attributes, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).AttributeWildcard, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).ContentModel, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).ContentTypeParticle, function);
CallForEveryElementInternal((schemaObject as XmlSchemaComplexType).Particle, function);
}
if (schemaObject is XmlSchemaElement)
{
CallForEveryElementInternal((schemaObject as XmlSchemaElement).Constraints, function);
CallForEveryElementInternal((schemaObject as XmlSchemaElement).ElementSchemaType, function);
CallForEveryElementInternal((schemaObject as XmlSchemaElement).SchemaType, function);
}
if (schemaObject is XmlSchemaGroup)
{
CallForEveryElementInternal((schemaObject as XmlSchemaGroup).Particle, function);
}
if (schemaObject is XmlSchemaGroupBase)
{
CallForEveryElementInternal((schemaObject as XmlSchemaGroupBase).Items, function);
}
if (schemaObject is XmlSchemaGroupRef)
{
CallForEveryElementInternal((schemaObject as XmlSchemaGroupRef).Particle, function);
}
if (schemaObject is XmlSchemaImport)
{
CallForEveryElementInternal((schemaObject as XmlSchemaImport).Annotation, function);
}
if (schemaObject is XmlSchemaInclude)
{
CallForEveryElementInternal((schemaObject as XmlSchemaInclude).Annotation, function);
}
if (schemaObject is XmlSchemaRedefine)
{
CallForEveryElementInternal((schemaObject as XmlSchemaRedefine).Items, function);
}
if (schemaObject is XmlSchemaSimpleContentExtension)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentExtension).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentExtension).Attributes, function);
}
if (schemaObject is XmlSchemaSimpleContentRestriction)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).AnyAttribute, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).Attributes, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).BaseType, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleContentRestriction).Facets, function);
}
if (schemaObject is XmlSchemaSimpleType)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleType).Content, function);
}
if (schemaObject is XmlSchemaSimpleTypeList)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeList).BaseItemType, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeList).ItemType, function);
}
if (schemaObject is XmlSchemaSimpleTypeRestriction)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeRestriction).BaseType, function);
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeRestriction).Facets, function);
}
if (schemaObject is XmlSchemaSimpleTypeUnion)
{
CallForEveryElementInternal((schemaObject as XmlSchemaSimpleTypeUnion).BaseTypes, function);
}
if (schemaObject is XmlSchemaType)
{
CallForEveryElementInternal((schemaObject as XmlSchemaType).BaseXmlSchemaType, function);
}
if (schemaObject is XmlSchemaAnnotated)
{
CallForEveryElementInternal((schemaObject as XmlSchemaAnnotated).Annotation, function);
}
if (schemaObject is XmlSchemaAnnotation)
{
CallForEveryElementInternal((schemaObject as XmlSchemaAnnotation).Items, function);
}
}
private static void CallForEveryElementInternal(XmlSchemaObjectCollection schemaObjectCol, SchemaObjectFunction function)
{
foreach (XmlSchemaObject xso in schemaObjectCol)
{
CallForEveryElementInternal(xso, function);
}
}
#endregion