Thursday 15 September 2011

c# - How to get first item from XmlSchema.Elements -



c# - How to get first item from XmlSchema.Elements -

i have xmlschema object. has elements property. i need first element it. cant figure how not writing foreach/break (which stupid). there nice way?

edit: way found : getenumerator/movenext/value;

edit2: 1 of ways cast xmlschema.elements.values (icollection) meaningful type utilize linq. problem cant find type. gettype gives me name: "system.xml.schema.xmlschemaobjecttable+valuescollection"

i dont see type in objectexplorer , cant cast it..

please reply if have working solution rather throwing in whatever comes head. thanks.

use linq on names or values of elements, depending on need

e.g.,

var v = yourobject.elements.names.oftype<xmlqualifiedname>().firstordefault(); var w = yourobject.elements.values.oftype<xmlschemaelement>().firstordefault();

===

edited: added oftype<> icollection<> instead of icollection possible utilize firstordefault

note: verified solution on illustration xmlschema http://msdn.microsoft.com/en-us/library/system.xml.schema.xmlschema%28v=vs.110%29.aspx, if element names or values have different type you'll need alter in oftype<>

c# .net xml xsd .net-4.5

No comments:

Post a Comment