vb.net - Use LINQ to query XML into an array using .ToArray() -
i vb6 veteran gradually coming grips vb.net. have working app trying configure using xml files (other basic app settings).
i want read elements single xml node within file , create array of strings. have searched site , others , have found many examples perform similar operations, can't quite grasp of (especially if question / answer in c#).
i'm no xml expert, know how select single node , write reasonably neat code iterate through child elements , manually build array using redim, , work fine. ... i'm trying learn new techniques specific example.
so ... seems maybe 2 or 3 compact statements should able write linq query , use .toarray() extension method populate array without resorting looping construct.
here representation of xml:
<?xml version="1.0"?> <targets> <target name="abc"> <index>0</index> <randoms> <string>index</string> <string>local</string> <string>news</string> <string>journal</string> <string>information</string> </randoms> </target> <target name="xyz"> <index>1</index> <randoms> <string>cat</string> <string>dog</string> <string>mouse</string> </randoms> </target> </targets>
the elements want build array "string" elements. in code use target index select appropriate node (there nodes other "randoms" not relevant example.
this solution creates object, saves xml file, sets nothing, loads xml file. make new console app , overwrite code see below.
bonus: toarray()
used prove class' list member ienumerable
imports system.xml.serialization imports system.io module module1 private targets xmltargets sub main() targets = new xmltargets targets.targets = new list(of xmltarget) dim t1 new xmltarget t1.name = "abc" t1.index = 0 t1.randoms = new random() t1.randoms.strings = new list(of [string]) dim s1 new [string]() s1.type = 1 s1.text = "index" dim s2 new [string]() s2.type = 2 s2.text = "value" t1.randoms.strings.add(s1) t1.randoms.strings.add(s2) dim t2 new xmltarget t2.name = "xyz" t2.index = 1 t2.randoms = new random() t2.randoms.strings = new list(of [string]) targets.targets.add(t1) targets.targets.add(t2) savexmlfile() targets = nothing loadxmlfile() dim ts = targets.targets.toarray() end sub private sub loadxmlfile() dim s new xmlserializer(gettype(xmltargets)) using sr new streamreader("test.xml") targets = s.deserialize(sr) end using end sub private sub savexmlfile() dim s new xmlserializer(gettype(xmltargets)) using sw new streamwriter("test.xml") s.serialize(sw, targets) end using end sub end module <xmlroot("targets")> public class xmltargets <xmlelement("target")> public property targets list(of xmltarget) end class <serializable> public class xmltarget <xmlattribute("name")> public property name string <xmlelement("index")> public property index integer <xmlelement("randoms")> public property randoms random end class <serializable> public class random <xmlelement("string")> public property strings list(of [string]) end class <serializable> public class [string] <xmlattribute("type")> public property [type] integer <xmltext> public property text string end class
this xml file software created. note: did not edit file outside of code above - created serializing class.
<?xml version="1.0" encoding="utf-8"?> <targets xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema"> <target name="abc"> <index>0</index> <randoms> <string type="1">index</string> <string type="2">value</string> </randoms> </target> <target name="xyz"> <index>1</index> <randoms /> </target> </targets>
post has been edited reflect change requested in comment below.
Comments
Post a Comment