2012-02-09 39 views
1

我只是有这个HTML电子邮件想通了,现在我需要的款式,以便我能得到我在这里表这2列之间的空间是我的代码:在C#中的XElement添加样式的HTML电子邮件

var html = new XDocument(
new XElement("html", 
    new XElement("body", 
     new XElement("h2", "Your entire inventory:"), 
     new XElement("table", 
      new XElement("tr", 
       new XElement("th", "Product"), 

       new XElement("th", "Quantity")), 
        new XElement("tr", 
       from item in prodList 
       select new XElement("td", item.ProductName, new XElement("td", item.Quantity), new XElement("tr"))) 
       )))); 

这是输出我得到:

Your entire inventory: 

       Product     Quantity 
Nissin rich & savory chicken bw - 6 pack 5 
The Zombie Survival Guide     3 
Maruchan Ramen Noodle Soup    5 
Generic Tomatoes, Whole     2 

所以我需要弄清楚如何将风格中的标签来获取填充/边框等在电子邮件

+0

我当然希望你得到的输出是XML,而不是您发布的文本。如果没有,为什么要使用'XDocument'? – Oded 2012-02-09 20:19:09

+0

我的最后一个问题是我从mysql获取数据到一个html电子邮件,导致我在那里。这是我最后的问题。 http://stackoverflow.com/questions/9167208/send-html-email-with-mysql-data-into-a-table-from-within-application/9169987#9169987 – 2012-02-09 20:21:49

回答

1

XElement结构相当坏了(我们定义中的其他元素tdtd元素tr元素);浏览器如何呈现结果是相当不可预测的。下面是它的外观:

<html> 
    <body> 
    <h2>Your entire inventory:</h2> 
    <table> 
     <tr> 
     <th>Product</th> 
     <th>Quantity</th> 
     </tr> 
     <tr> 
     <td>Nissin rich &amp; savory chicken bw - 6 pack<td>5</td><tr /></td> 
     <td>The Zombie Survival Guide<td>3</td><tr /></td> 
     <td>Maruchan Ramen Noodle Soup<td>5</td><tr /></td> 
     <td>Generic Tomatoes, Whole<td>2</td><tr /></td> 
     </tr> 
    </table> 
    </body> 
</html> 

要开始,你应该修复你的HTML代码(见下文)。

既然你打算在电子邮件中使用你的HTML,你最好避开嵌入式样式表(尽管他们呼吁避免代码重复)。某些电子邮件(网络)客户端(最着名的是Gmail)会忽略嵌入式样式表(请参阅Using CSS and HTML in Email Newsletters)。在大多数情况下,使用HTML电子邮件的内联样式更为安全。

要在两列之间引入一些间距,您可以在左列中的所有单元格上定义一行style="padding-right:50px;"属性;这将确保最长的产品名称和数量列之间有50个像素的空白。

var html = new XDocument(
    new XElement("html", 
     new XElement("body", 
      new XElement("h2", "Your entire inventory:"), 
      new XElement("table", 
       new XElement("tr", 
       new XElement("th", "Product", 
        new XAttribute("style", "padding-right:50px;")), 
       new XElement("th", "Quantity")), 
      from item in prodList 
      select new XElement("tr", 
       new XElement("td", item.ProductName, 
        new XAttribute("style", "padding-right:50px;")), 
       new XElement("td", item.Quantity)))))); 

上面的代码会产生:

<html> 
    <body> 
    <h2>Your entire inventory:</h2> 
    <table> 
     <tr> 
     <th style="padding-right:50px;">Product</th> 
     <th>Quantity</th> 
     </tr> 
     <tr> 
     <td style="padding-right:50px;">Nissin rich &amp; savory chicken bw - 6 pack</td> 
     <td>5</td> 
     </tr> 
     <tr> 
     <td style="padding-right:50px;">The Zombie Survival Guide</td> 
     <td>3</td> 
     </tr> 
     <tr> 
     <td style="padding-right:50px;">Maruchan Ramen Noodle Soup</td> 
     <td>5</td> 
     </tr> 
     <tr> 
     <td style="padding-right:50px;">Generic Tomatoes, Whole</td> 
     <td>2</td> 
     </tr> 
    </table> 
    </body> 
</html> 
2

定义的样式HTML元素通常使用CSS:

new XElement("html", 
    new XElement("head", 
     new XElement("style", 
      new XAttribute("type", "text/css"), 
      "td { border: 1px solid red; }" 
     ) 
    ), 
    new XElement("body", ... 
+0

以及我知道这将是CSS,但我didn不知道如何告诉C#我正在使用CSS。但现在我感谢XAttribute让我们试试这个,我会尽快回复你。谢谢 – 2012-02-09 20:50:02

+0

小心;一些电子邮件(网络)客户端,尤其是Gmail,忽略了HTML邮件中的嵌入式样式表。 – Douglas 2012-02-09 20:59:12

+0

我使用gmail来实际查看html。并在铬内。谢谢你的建议 – 2012-02-09 21:08:15

相关问题