linq to xml - Convert XML file with duplicate tags to CSV -
i'm looking way convert xml file csv file tag names headers. however, problem xml file has duplicate tags makes process of parsing file , "finding" elements difficult. problem around <file>
tags.
here's snippet of file i'm trying convert. also, bear in mind <file>
tags dynamic, can have 0 or 10 <file>
tags.
<program> <programname>enrolled nursing assistant</programname> <category>nursing</category> <credential>enrolled nurse</credential> <programlevel>-</programlevel> <startdate>27-09-2004</startdate> <completiondate>05-05-2006</completiondate> <institution> <institutiontype>college</institutiontype> <schoolname>excelsior community college(main)</schoolname> <primarylanguage>english</primarylanguage> <languageofinstruction> <theory>english</theory> <clinical>english</clinical> </languageofinstruction> <address> <streetaddress1>-</streetaddress1> <streetaddress2>-</streetaddress2> <pobox>-</pobox> <city>-</city> <state>-</state> <country iso-code='876'>jamaica</country> <postalcode>-</postalcode> </address> </institution> <documents> <document> <documenttype>transcript</documenttype> <documentnumber>001</documentnumber> <issuedfrom iso-code='876'>country</issuedfrom> <dateissued>-</dateissued> <receiveddate>28-05-2014</receiveddate> <files> <file> <name>001.tiff</name> <path>images\education</path> <extension>tiff</extension> <size>36000</size> <lastmodifieddate>28-05-2014</lastmodifieddate> </file> <file> <name>7002.tiff</name> <path>images\education</path> <extension>tiff</extension> <size>38000</size> <lastmodifieddate>28-05-2014</lastmodifieddate> </file> <file> <name>003.tiff</name> <path>images\education</path> <extension>tiff</extension> <size>50000</size> <lastmodifieddate>28-05-2014</lastmodifieddate> </file> </files> </document> </documents> </program>
i have solution convert csv doesn't handle duplicated tags. keeps using details of first <file>
tag. so, in csv file there tags 3 file have details of first tag.
var xml = xdocument.load(@"c:/path/7123451_53957.xml"); string program = "program"; func<xdocument, ienumerable<string>> getfields = xd => xd .descendants(program) .selectmany(d => d.descendants()) .select(e => e.name.tostring()); var headers = string.join(",", getfields(xml) .select(f => csvformat(f))); var programquery = (from programs in xml.descendants(program) select string.join(",", getfields(xml) .select(f => programs.descendants(f).any() ? programs.descendants(f).first().value : "") .select(x => csvformat(x)))) .tolist();
i believe problem around programs.descendants(f).first().value
part.
Comments
Post a Comment