2013-10-25 32 views
0

我有一個從XML創建C#類的XSLT。我正在使用從XML生成C#代碼:如何輸出'<' and '>'?

<xsl:output method="text"/> 

創建一個文本文件。基本上我從http://docstore.mik.ua/orelly/xml/jxslt/ch08_05.htm獲取代碼並將其修改爲輸出C#代碼。

但現在我需要輸出類似

class PersonValidator : AbstractValidator<Person> 
{ 
    public PersonValidator() 
    { 
     RuleFor(obj => obj.LastName).NotEmpty(); 
     ... 
    } 
} 

...但只要我想輸出「<」或「>」我得到「& LT;」和'& gt;'。

有沒有簡單的方法來完成這個?

編輯:

這裏是我當前的XSLT(中CDATA不工作!):

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet version="1.0" 
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="text"/> 
    <xsl:variable name="className" select="/Class/@Name"/> 
    <xsl:variable name="entityId" select="(//Property)[1]/@Name"/>  
    <xsl:template match="/Class">using System; 
using System.Linq; 
using Core.Common.Core; 
using FluentValidation; 
using System.Collections.Generic; 

namespace PersonDosimetry.Client.Entities.Constants 
{ 
    public class <xsl:value-of select="$className"/> 
     <xsl:text> : ObjectBase</xsl:text> 
    { 
    <xsl:apply-templates select="Property" mode="generateField"/> 
     <xsl:text>  
     #region Business-mapped Properties 
     </xsl:text> 
     <xsl:apply-templates select="Property" mode="generateProperty"/> 
     #endregion 

     #region Validation 

     class <xsl:value-of select="$className"/>Validator : AbstractValidator<![CDATA[<]]><xsl:value-of select="$className"/><![CDATA[>]]> 
     { 
      public <xsl:value-of select="$className"/>Validator() 
      {    
       //RuleFor(obj =<![CDATA[>]]> obj.LastName).NotEmpty(); 
      } 
     } 

     protected override IValidator GetValidator() 
     { 
      return new <xsl:value-of select="$className"/>Validator(); 
     } 

     #endregion 
    } 
} 
</xsl:template> 

    <!-- 
    ***************************************************************** 
    ** Generate a private field declaration. 
    **************************************************************--> 
    <xsl:template match="Property" mode="generateField"><xsl:text> </xsl:text> 
    <xsl:value-of select="@Type"/> 
    <xsl:text> _</xsl:text> 
    <xsl:value-of select="@Name"/>; 
    </xsl:template> 

    <!-- 
    ***************************************************************** 
    ** Generate a "get" method for a property. 
    **************************************************************--> 
    <xsl:template match="Property" mode="generateProperty"> 
     public <xsl:value-of select="@Type"/><xsl:text> </xsl:text><xsl:value-of select="@Name"/> 
     { 
      get { return _<xsl:value-of select="@Name"/>; } 
      set 
      { 
       if (_<xsl:value-of select="@Name"/> != value) 
       { 
        _<xsl:value-of select="@Name"/> = value; 
        OnPropertyChanged(); 
       } 
      } 
     } 
    </xsl:template> 
</xsl:stylesheet> 

我的示例XML:

<?xml version="1.0"?> 
<Class Name="Person"> 
    <Property Name="PersonId" Type="Int32" /> 
    <Property Name="FirstNames" Type="String" /> 
    <Property Name="LastName" Type="String" /> 
    <Property Name="GenderTypeId" Type="Int32" /> 
    <Property Name="BirthDate" Type="DateTime" /> 
    <Property Name="InsuranceNumber" Type="String" /> 
    <Property Name="Country" Type="String" /> 
    <Property Name="Beruf" Type="String" /> 
</Class> 
+0

[CDATA](http://en.wikipedia.org/wiki/CDATA)可能會可能工作最 –

+1

你能向我們展示您當前正試圖輸出'<' or '>'的XSLT代碼?謝謝! –

+0

是的,請向我們展示您的XSLT並告訴我們您使用的是哪種XSLT處理器。如果您使用'xsl:output method =「text」',那麼輸出'<' and '>'應該很容易,除非您使用的是不合標準的XSLT處理器。 – JLRishe

回答

1

問題是XSLT處理器我使用(版本從這裏:http://www.codeproject.com/Articles/8823/A-better-MSXSL-EXE-Adding-the-ability-to-Transform)。我開始使用它,因爲我的代碼生成器需要處理一些文件,而且我的印象是這沒有什麼不同於MSXSL。

我下載了MSXSL,一切都順利使用CDATA。

(A後續的問題是:??我該怎麼做正確的轉換編程在.NET或有更好的XSL轉換庫,我可以使用)

更新

JLRishe的評論讓我朝着正確的方向前進。我改變上述CodeProject上的代碼下面,現在的代碼生成作品:

class Program 
{ 
    static void Main(string[] args) 
    { 
     if (args.Length != 3) 
     { 
      Console.WriteLine("You have not entered the correct parameters"); 
      return; 
     } 
     string xmlfile = args[0]; 
     string xslfile = args[1]; 
     string outfile = args[2]; 
     try 
     { 
      XPathDocument doc = new XPathDocument(xmlfile); 
      XslCompiledTransform transform = new XslCompiledTransform(); 
      transform.Load(xslfile); 
      XmlWriter writer = XmlWriter.Create(outfile, transform.OutputSettings); 
      transform.Transform(doc, writer); 
      writer.Close(); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine(e.StackTrace); 
     } 
    } 
}  
+1

在.NET中,您應該使用[XslCompiledTransform類](http://msdn.microsoft.com/zh-cn/library/system.xml.xsl.xslcompiledtransform.aspx)。它支持嵌入式腳本,如果你出於某種原因需要這些。另外,您不需要使用CDATA來完成此任務。如果在XSLT中使用'<'和'>',只要輸出方法是'text',它們應該輸出爲'<' and '>'。 – JLRishe

-2

你應該看看Xml Serialization。您可以將對象轉換爲xml並返回到c#對象。這使我可以輕鬆地將任何對象存儲爲xml,並快速將其反序列化爲對象。

例如,我的對象被稱爲 「MyObject來」:

public object DeserializeXmlMyObj(String xml) 
    { 
     try 
     { 
      XmlSerializer serializer = new XmlSerializer(typeof(MyObject)); 

      MyObject obj = null; 
      using (StringReader reader = new StringReader(xml)) 
      { 
       obj = (MyObject)serializer.Deserialize(reader); 
      } 
      return obj ; 
     } 
     catch (Exception ex) 
     { 

      return null; 
     } 

    } 
+0

問題是關於將XML轉換爲C#*代碼*,它不是關於反序列化對象。 – svick

+0

@svick - 然而,當你在上面的例子中反序列化xml時,它會創建c#對象。雖然,這可能不是一個直接的答案,但它可能是他甚至沒有考慮的解決方案。我的意圖是鼓勵一種替代方法。 – PhillyNJ

+0

這沒有任何意義。如果你試圖生成C#類,然後你可以使用你的代碼,那麼一些反序列化的對象根本就不會做。 – svick