你要创建一个XmlSchemaSet
,阅读在你的模式中,然后编译它来创建一个信息集。一旦你做到了这一点,你就可以开始通过文件迭代
XmlSchemaElement root = _schema.Items[0] as XmlSchemaElement;
XmlSchemaSequence children = ((XmlSchemaComplexType)root.ElementSchemaType).ContentTypeParticle as XmlSchemaSequence;
foreach(XmlSchemaObject child in children.Items.OfType<XmlSchemaElement>()) {
XmlSchemaComplexType type = child.ElementSchemaType as XmlSchemaComplexType;
if(type == null) {
// It's a simple type, no sub-elements.
} else {
if(type.Attributes.Count > 0) {
// Handle declared attributes -- use type.AttributeUsers for inherited ones
}
XmlSchemaSequence grandchildren = type.ContentTypeParticle as XmlSchemaSequence;
if(grandchildren != null) {
foreach(XmlSchemaObject xso in grandchildren.Items) {
if(xso.GetType().Equals(typeof(XmlSchemaElement))) {
// Do something with an element.
} else if(xso.GetType().Equals(typeof(XmlSchemaSequence))) {
// Iterate across the sequence.
} else if(xso.GetType().Equals(typeof(XmlSchemaAny))) {
// Good luck with this one!
} else if(xso.GetType().Equals(typeof(XmlSchemaChoice))) {
foreach(XmlSchemaObject o in ((XmlSchemaChoice)xso).Items) {
// Rinse, repeat...
}
}
}
}
}
}
显然,你会想要把所有的孩子处理的东西,在一个单独的方法和递归调用它,但是这应该告诉你的一般流程。
来源
2010-07-16 17:29:58
TMN
不幸的是,我对xml不熟悉。 – 2010-07-16 16:22:15
@Scott:掌握如何解析XML(尤其是基本的XPath使用)的基本知识是值得学习的,至少在您能够获得基本查询工作的地方,如果您在前面有参考指南。这不是特别复杂。对于至少将其中一种输出类型使用XML的应用程序来说,相对比较常见(尽管像SOAP这样的东西有VS封装器,所以通常可以忽略XML部分)。 – Brian 2010-07-16 16:40:00