简体   繁体   中英

How to get first item from XmlSchema.Elements

I have XmlSchema object. Which has Elements property. I need a first element from it. I cant figure how to get it not writing foreach/break (which would look stupid). Is there a nice way?

EDIT: the only way I found is : getenumerator/movenext/value;

EDIT2: one of the ways is to cast XmlSchema.Elements.Values (ICollection) to a meaningful type to use with Linq. The problem is that I cant find that type. The GetType gives me name: "System.Xml.Schema.XmlSchemaObjectTable+ValuesCollection"

I dont see this type in the ObjectExplorer and I cant cast to it..

Please answer if you have working solution rather than just throwing in whatever comes to your head. Thanks.

Use linq on the Names or Values of the elements, depending on what you need

Eg,

var v = yourObject.Elements.Names.OfType<XmlQualifiedName>().FirstOrDefault();
var w = yourObject.Elements.Values.OfType<XmlSchemaElement>().FirstOrDefault();

===

Edited: Added OfType<> to get an ICollection<> instead of an ICollection so that it is possible to use FirstOrDefault

Note: I verified this solution on the example XmlSchema from http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema%28v=vs.110%29.aspx , if your element names or values have a different type you'll need to change that in the OfType<>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM