且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中将XSLT应用于XML

更新时间:2023-12-01 22:41:28

为什么不只是将文件作为文本加载,并对其进行适当的Replace调用:

Why don''t you just load the file as text, and do approproriate Replace calls on it:

string myXML = // load the file into this string
myXML = my.XML.Replace("<item>", "<NewItemName>").Replace("</item>", "</NewItemName>");
myXML = my.XML.Replace("<itemdetail>", "<NewItemDetailName>").Replace("</itemdetail>", "</NewItemDetailName>");
myXML = my.XML.Replace("<detail>", "<NewDetailName>").Replace("</detail>", "</NewDetailName>");
myXML = my.XML.Replace("<Header>", "<NewHeaderName>").Replace("</Header>", "</NewHeaderName>");



当然,您不得不怀疑您为什么要这样做.从编程的角度来看,这是完全不必要的.该文件就是它的样子.



Of course, one has to wonder why you''re doing this. It''s completely unneccessary from a programming point of view. The file is what it is.


我仍然认为将其作为文本加载并基于app.config中的标签名称进行替换比较容易,但是通过一些小搜索,我发现了这个示例,而您可能必须:

I still think loading it as text and doing replace based on tag names in a app.config is easier, but with some minor googling, I found this example, and you could have to:

<xsl:template match="item">
   <xsl:element name="NewItemName">
     <xsl:apply-templates select="@*|node()"/>
   </xsl:element>
</xsl:template>



如果您需要更多示例,则google是您的朋友.我搜索了使用xslt更改标签",并返回了700多个结果.



If you want more examples, google is your friend. I searched for "using xslt to change tags" and got back over 700 THOUSAND results.