在做一个ASP的维护项目,变更的代码要加注释,纳品前要达到目标值(目标值是按照千行代码统计的)。几十个文件手动统计太累了,于是写了一个统计代码的工具,其中一个功能是将统计结果保存成XML文件,为了方便查看又写了一XSLT。

注释风格:
1使用.NET生成XML时导入XSLT' Start(Mod) 2005/12/02 谷常生 [00000007]
2使用.NET生成XML时导入XSLT        nShishaMeisaiNgFlg = 1
3使用.NET生成XML时导入XSLT' End  (Mod) 2005/12/02 谷常生 [00000007]

生成的XML格式如下:
 1使用.NET生成XML时导入XSLT<?xml version="1.0" encoding="utf-8" standalone="yes"?>
 2使用.NET生成XML时导入XSLT<?xml-stylesheet type="text/xsl" href="gucs.xslt"?>
 3使用.NET生成XML时导入XSLT<CountInformation>
 4使用.NET生成XML时导入XSLT  <Item>
 5使用.NET生成XML时导入XSLT    <FileName>E:\project\PRユーザ管理変更\03 製造\01 ソース\In-Process\PR_PORTAL\PR_USER\SCRIPT\PRU_AdmRegQreLstFtr.asp</FileName>
 6使用.NET生成XML时导入XSLT    <Total>51</Total>
 7使用.NET生成XML时导入XSLT    <CodeOnly>31</CodeOnly>
 8使用.NET生成XML时导入XSLT    <Comment>13</Comment>
 9使用.NET生成XML时导入XSLT    <Blank>7</Blank>
10使用.NET生成XML时导入XSLT    <CommentCode>2</CommentCode>
11使用.NET生成XML时导入XSLT  </Item>
12使用.NET生成XML时导入XSLT  <Item>
13使用.NET生成XML时导入XSLT    <FileName>E:\project\PRユーザ管理変更\03 製造\01 ソース\In-Process\PR_PORTAL\PR_USER\SCRIPT\PRU_CommonFunction.inc</FileName>
14使用.NET生成XML时导入XSLT    <Total>1163</Total>
15使用.NET生成XML时导入XSLT    <CodeOnly>655</CodeOnly>
16使用.NET生成XML时导入XSLT    <Comment>330</Comment>
17使用.NET生成XML时导入XSLT    <Blank>178</Blank>
18使用.NET生成XML时导入XSLT    <CommentCode>390</CommentCode>
19使用.NET生成XML时导入XSLT  </Item>
20使用.NET生成XML时导入XSLT  <Item>
21使用.NET生成XML时导入XSLT    <FileName>Total</FileName>
22使用.NET生成XML时导入XSLT    <Total>1214</Total>
23使用.NET生成XML时导入XSLT    <CodeOnly>686</CodeOnly>
24使用.NET生成XML时导入XSLT    <Comment>343</Comment>
25使用.NET生成XML时导入XSLT    <Blank>185</Blank>
26使用.NET生成XML时导入XSLT    <CommentCode>392</CommentCode>
27使用.NET生成XML时导入XSLT  </Item>
28使用.NET生成XML时导入XSLT</CountInformation>

由于使用XmlDocument生成XML,不可能手动写字符串,只能使用系统提供的对象,找了半天的资料,最后还是在MSDN中找到了,以下为实现代码:

1使用.NET生成XML时导入XSLTXmlProcessingInstruction xsl = doc.CreateProcessingInstruction("xml-stylesheet""type=\"text/xsl\" href=\"gucs.xslt\"");
2使用.NET生成XML时导入XSLTdoc.InsertBefore(xsl, doc.DocumentElement);

这两行代码,就生成了这条XML语句:
1使用.NET生成XML时导入XSLT<?xml-stylesheet type="text/xsl" href="./gucs.xslt"?>

使用XmlProcessingInstruction就可以了,就是这么方便,呵呵。

相关文章: