Recently, I am working on a task item. I need to check the hard code labels in reports in the Reporting Services (2003/2005) format and convert them to the tables in the database for use, at that time, the requirement was to first generate a report. I thought that if there were many reports, they would only be done manually. It would be strange if they were not exhausted. I learned it. use this to implement the LINQ to XML features in net3.5.
First, we have to have an idea. We all know that micorsoft's reporting services, regardless of the RDL file 2003 or 2005, is actually an XML file, and the node format inside is roughly the same, labels in XML are all <textbox>... </Textbox>
Ideas:
1. Find all textbox
2. Find that the content of these textbox contains hardcode in Chinese or English (matching with their regular expressions. In this example, only English is used)
3. automatically generate a non-repeated label ID to the hardcode label as needed, and generate an insert statement based on the resource table results.
4. Replace the hard code in the XML content of the report with the added label ID.
5. You can also find the label stored procedure under the datasets node and add a new field to it. However, if you forget it, open the report directly and refresh the data source.
The Code is as follows:
Private void button#click (Object sender, eventargs e) <br/>{< br/> // <br/> xnamespace NS = "http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition"; <br/> If (file. exists ("output2.txt") <br/>{< br/> file. delete ("output2.txt"); <br/>}</P> <p> using (filestream FS = file. open ("output2.txt", filemode. openorcreate, fileaccess. write, fileshare. none) <br/>{< br/> xnamespace NS2. = "Urn: Schemas-Microsoft-com: Office: spreadsheet"; <br/> xnamespace NSSs = "urn: Schemas-Microsoft-com: Office: spreadsheet "; </P> <p> // file in Dir. getfiles ("*. RDL ") <br/> string filename = application. startuppath + @ "/staffinjuryreport. RDL "; <br/> xelement elereport = xelement. load (filename ); </P> <p> // RegEx Reg = new RegEx (@ "[<] value [>] [^ =] * [<]/value [>]"); <br/> RegEx Reg = new RegEx (@ "[<] value [>] [^ =] * [<]/Value [>] "); </P> <p> ienumerable <xelement> tblist = <br/> from itemfield in elereport. descendants (NS + "textbox") <br/> // Where Reg. ismatch (itemfield. elements (NS + "value "). first (). value) = true <br/> select itemfield; </P> <p> string linestr = ""; <br/> string hardcodestring = ""; <br/> string reportlabelid = ""; <br/> string needreplacevalue = ""; <br/> system. text. stringbuilder sb = new Stringbuilder (); <br/> dictionary <string, string> existsdict = new dictionary <string, string> (); <br/> dictionary <string, string> existslabeliddict = new dictionary <string, string> (); <br/> int duplicatelabelidindex = 0; <br/> // insert into odm_cdreportlabel ([reportlabelid], [code1], [code2], [code3], [reportid], [version]) values ('lblaccident', n' accident ', n' unexpected ', N 'accidents ', N 'odmstaffinjury', 'kkkkkk ') <br/> Foreach (VAR item in tblist) <br/>{< br/> // reportlabelid = (string) item. element (NS + "datafield"); <br/> // -------------- 1. -<br/> hardcodestring = string. isnullorempty (string) item. element (NS + "value "))? "": (String) item. element (NS + "value"); <br/> If (hardcodestring. length> 0) <br/>{< br/> // string Ss = item. value; <br/> // ---- begin ----------- hardcode in English ----------------------------------------------------------- <br/> If (Reg. ismatch (item. tostring () <br/>{< br/> // you can determine whether the specified result exists. <br/> If (existsdict. values. contains (hardcodestring) <br/>{< br/> reportlabelid = existsdict. where (CA => CA. value = hardcodestring ). first (). key; <br/>}< br/> else <br/> {<br/> // reportlabelid = ""; <br/> // hardcodestring = ""; </P> <p> reportlabelid = "LBL" + (hardcodestring. length <5? Hardcodestring. replace ("", ""): hardcodestring. substring (0, 5 ). replace ("", ""); <br/> reportlabelid = reportlabelid. replace ("'",""). replace (":",""). replace (". ",""). replace ("I ",""). replace ("&",""). replace ("'",""). replace ("II ",""). replace (")",""). replace ("(", ""); <br/> // dB <br/> If (this. omisdbdataset. odm_cdreportlabel.count> 0) <br/>{< br/> If (this. omisdbdataset. odm_cdreportlabel.w Here (CA => Ca. reportlabelid = reportlabelid ). any () <br/>{< br/> reportlabelid = reportlabelid + "forstaffinjury "; <br/>}</P> <p> // check whether the reportlabelid is repeated. <br/> // existslabeliddict <br/> If (existslabeliddict. values. contains (reportlabelid) <br/>{< br/> reportlabelid = reportlabelid + duplicatelabelidindex. tostring (); <br/>}</P> <p> linestr = @ "insert into odm_cdreportlabel ([reportlabe Lid], [code1], [code2], [code3], [reportid], [version]) values ('"<br/> +" "+ reportlabelid +"' "<br/> +", N' "+ hardcodestring. replace ("'", "'' ") +"' "<br/> +", N' "+ hardcodestring. replace ("'", "'' ") +"' "<br/> +", N' "+ hardcodestring. replace ("'", "'' ") +"' "<br/> +", n' odmstaffinjury '"<br/> +", 'mtrc ') "<br/> +"/R/N "; <br/> byte [] info = new utf8encoding (true ). getbytes (linestr); <br/> // Dd some information to the file. <br/> FS. write (Info, 0, info. length); </P> <p> Sb. append ("'" + reportlabelid + "',"); <br/> existslabeliddict. add (reportlabelid, reportlabelid); <br/> existsdict. add (reportlabelid, hardcodestring); <br/>}</P> <p> // ------------------------ 2. -- <br/> // <value> = first (Fields! Wasmahcineinmotion. value, "getrptlabel") </value> <br/> // replace <br/> needreplacevalue = "= first (Fields! "+ Reportlabelid + ". value,/"getrptlabel/") "; <br/> item. element (NS + "value "). value = needreplacevalue; <br/>}</P> <p> // ---- end ----------- explanation of hardcode in English <br/>}</P> <p> duplicatelabelidindex ++; <br/>}</P> <p> byte [] info2 = new utf8encoding (true ). getbytes (sb. tostring (); <br/> FS. write (info2, 0, info2.length); </P> <p> elereport. save ("sta Ffinjuryreport_finishedreplace.rdl "); </P> <p> MessageBox. Show (" finished! "); <Br/>}< br/>}
Note: Of course, it can be more automatic to traverse multiple files and so on, but this convenient small program is enough, and it is a method to break it down.