2017-12-21 2 views
0

私はhtmlファイルを生成し、これを文字列で返すc#クラスを作成しました。方法:C#クラスライブラリでプレビューして印刷しますか?

しかし、私はこの文書をプレビューするHTMLを追加する必要があります(ブラウザに置くことができます)。

マイコード:

public class Class1 
{ 
    public string HTMGenerator() 
    { 
     string html = "<p>test</p>"; 

     var xDocument = new XDocument(
      new XDocumentType("html", null, null, null), 
      new XElement("html", 
       new XElement("head"), 
       new XElement("body" 
         XElement.Parse(html)), 
       ) 
      ); 

     var settings = new XmlWriterSettings 
     { 
      OmitXmlDeclaration = true, 
      Indent = true, 
      IndentChars = "\t" 
     }; 

     using (var sw = new StringWriter()) 
     { 
      using (var writer = XmlWriter.Create(sw, settings)) 
      { 
       xDocument.WriteTo(writer); 
      } 

      return sw.ToString(); 
     } 
    } 
} 
+0

なぜ( 'リターンxDocument.ToStringを書きません) ' - あなたのストリングライターコードと同じことをします。 – Enigmativity

+0

閲覧用には、Webブラウザコンポーネントがあります。フォームにドロップし、文字列を入力します。 –

答えて

0

はこのお試しください:

string filename = "Your FileName.html"; 
File.WriteAllText(filename, HTMGenerator()); 
Process.Start(filename); 

をここで私はあなたのコードを記述します方法は次のとおりです。

void Main() 
{ 
    var filename ="Your File.html"; 
    var instance = new Class1(); 
    var document = instance.HTMGenerator(); 
    document.Save(filename); 
    Process.Start(filename); 
} 

public class Class1 
{ 
    public XDocument HTMGenerator() 
    { 
     string html = "<p>test</p>"; 

     var xDocument = 
      new XDocument(
       new XDocumentType("html", null, null, null), 
       new XElement(
        "html", 
        new XElement("head"), 
        new XElement(
         "body", 
         XElement.Parse(html)))); 

     return xDocument; 
    } 
} 
+0

どこに追加すればよいですか?私の.dllに? – rayman22

+0

これは論理的に意味があるところに追加する必要があります。 – Enigmativity

+0

私の.dllクラスファイルにプレビューを追加できますか? 文字列を参照するのはメインプログラムだけですか? – rayman22

関連する問題