so have device has inbuilt logger program generates status messages device , keeps pushing them .txt file. these messages include information device status, network status amongst many other things. data in file looks following:
<xml><dstatus>1,4,7,,5</dstatus><event> hello,there,my,name,is,jack,</event> last,name,missing,above <anothertag>3,6,7,,8,4</anothertag> </xml> <xml><dstatus>1,5,7,,3</dstatus><event>hello,there,my,name,is,mary,jane</event> last,name,not,missing,above<anothertag>3,6,7,,8,4</anothertag></xml> ... goes on note not formed xml. also, 1 element can have multiple parameters , can have blanks... example: <networkstat>1,456,3,6,,7</networkstat> objective is write in c# wpf, take text file, process data in , create .csv file each event per line. example, above given brief example, first line in csv file be:
1,4,7,,5,hello,there,my,name,is,jack,,last,name,missing,above,3,6,7,,8,4 also, not need using basic c#. know how read file, etc.. have no clue how approach problem in regards parsing , processing , converting. i'm new c# i'm not sure direction go. appreciated!
due non standard format had switch xml linq solution standard xml solution. linq doesn't support text strings not in tags.
using system; using system.collections.generic; using system.linq; using system.text; using system.io; using system.xml; using system.xml.linq; namespace consoleapplication1 { class program { const string filename = @"c:\temp\test.csv"; static void main(string[] args) { string input = "<xml><dstatus>1,4,7,,5</dstatus><event> hello,there,my,name,is,jack,</event>" + "last,name,missing,above <anothertag>3,6,7,,8,4</anothertag> </xml>" + "<xml><dstatus>1,5,7,,3</dstatus><event>hello,there,my,name,is,mary,jane</event>" + "last,name,not,missing,above<anothertag>3,6,7,,8,4</anothertag></xml>"; input = "<root>" + input + "</root>"; xmldocument doc = new xmldocument(); doc.loadxml(input); streamwriter writer = new streamwriter(filename); xmlnodelist rows = doc.getelementsbytagname("xml"); foreach (xmlnode row in rows) { list<string> children = new list<string>(); foreach (xmlnode child in row.childnodes) { children.add(child.innertext.trim()); } writer.writeline(string.join(",", children.toarray())); } writer.flush(); writer.close(); } } }
Comments
Post a Comment