wpf - What is the most efficient way to extract information from a text file formatted like this -
consider text file stored in online location looks this:
;aiu; [myeditor45] name = myeditor 4.5 url = http://www.myeditor.com/download/myeditor.msi size = 3023788 description = latest version of myeditor feature = support other file types feature1 = support different encodings bugfix = fix bug file open bugfix1 = fix crash when opening large files bugfix2 = fix bug search in file feature filepath = %programfiles%\myeditor\myeditor.exe version = 4.5
which details information possible update application user download. want load stream reader, parse , build list of features, bugfixes etc display end user in wpf list box.
i have following piece of code gets text file (first extracting location local ini file , loads streamreader. @ least works although know there no error checking @ present, want establish efficient way parse first. 1 of these files unlikely ever exceed more 250 - 400 lines of text.
dim updateurl string = geturl() dim client new webclient() using mystreamreader new streamreader(client.openread($"{updateurl}")) while not mystreamreader.endofstream dim line string = mystreamreader.readline if line.contains("=") dim p string() = line.split(new char() {"="c}) if p(0).contains("bugfix") messagebox.show($" {p(1)}") end if end if end while end using
specifically i'm looking collate information features, bugfixes , enhancements. whilst construct in effect rather messy if statement feel sure there must more efficient way , possibly involving linq. i'd welcome suggestions.
i have added wpf tag on off chance reading more experience of displaying information in wpf listboxes have might spot way define info i'm after in such way displayed in wpf list box in 3 sections (features, enhancements , bugfixes).
dom, here answer in c#. try convert vb.net momentarily. first, since file small, read of list of strings. select strings contain "=" , parse them data items can used. code return set of data items can display like. if have linqpad, can test thecode below, or have code here: dotnetfiddle
here vb.net version: vb.net dotnetfiddle
imports system imports system.collections.generic imports system.linq public class program public sub main() dim filecontent list(of string) = getfilecontent() dim dataitems = filecontent.where(function(c) c.contains("=")).[select](function(c) getdataitem(c)) dataitems.dump() end sub public function getfilecontent() list(of string) dim contentlist new list(of string)() contentlist.add("sb.app; aiu;") contentlist.add("") contentlist.add("[myeditor45]") contentlist.add("name = myeditor 4.5") contentlist.add("url = http://www.myeditor.com/download/myeditor.msi") contentlist.add("size = 3023788") contentlist.add("description = latest version of myeditor") contentlist.add("feature = support other file types") contentlist.add("feature1 = support different encodings") contentlist.add("bugfix = fix bug file open") contentlist.add("bugfix1 = fix crash when opening large files") contentlist.add("bugfix2 = fix bug search in file feature") contentlist.add("filepath = % programfiles %\myeditor\myeditor.exe") contentlist.add("version = 4.5") return contentlist end function public function getdataitem(value string) dataitem dim parts = value.split("=", 2, stringsplitoptions.none) dim dataitem = new dataitem() dataitem.datatype = parts(0).trim() dataitem.data = parts(1).trim() return dataitem end function end class public class dataitem public datatype string public data string end class
or, in c#:
void main() { list<string> filecontent = getfilecontent(); var dataitems = filecontent.where(c => c.contains("=")) .select(c => getdataitem(c)); dataitems.dump(); } public list<string> getfilecontent() { list<string> contentlist = new list<string>(); contentlist.add("sb.app; aiu;"); contentlist.add(""); contentlist.add("[myeditor45]"); contentlist.add("name = myeditor 4.5"); contentlist.add("url = http://www.myeditor.com/download/myeditor.msi"); contentlist.add("size = 3023788"); contentlist.add("description = latest version of myeditor"); contentlist.add("feature = support other file types"); contentlist.add("feature1 = support different encodings"); contentlist.add("bugfix = fix bug file open"); contentlist.add("bugfix1 = fix crash when opening large files"); contentlist.add("bugfix2 = fix bug search in file feature"); contentlist.add("filepath = % programfiles %\\myeditor\\myeditor.exe"); contentlist.add("version = 4.5"); return contentlist; } public dataitem getdataitem(string value) { var parts = value.split('='); var dataitem = new dataitem() { datatype = parts[0], data = parts[1] }; return dataitem; } public class dataitem { public string datatype; public string data; }
Comments
Post a Comment