html效果截图:

1、首先分析html代码结构:

结果如图:

2、调用接口返回的数据格式:

3、由第一步可看出每2个数据为一行并排显示,后台返回的数据总数可能为奇数个或偶数个,对应该生成的xml结构:

4、对应的xslt文件代码

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
><xsl:output method="html" indent="yes"/><xsl:template match="/root"><html lang="en"><head><title>REAL ESTATE</title><meta charset="utf-8"></meta><meta name="viewport" content="width=device-width, initial-scale=1.0" /><link rel="shortcut icon" href="http://joannarealestate.com.cn/images/jre_logo.png" type="image/png"></link></head><style>th.c20 {font-size: 0pt;line-height: 0pt;padding: 0;margin: 0;font-weight: normal;vertical-align: top;}table.c19 {border-bottom: 1px solid rgb(219, 219, 219);}td.c18 {background-color: #FFFFFF;/*border: 1px solid #232129;*/}td.c17 {color: #232129;font-family: Arial, sans-serif;font-size: 12px;width: 148px;height: 40px;line-height: 16px;text-align: center;}a.c16 {color: #232129;text-decoration: none;}img.c15 {margin: 0;padding: 0;max-width: 25px;}img.c14 {margin: 0;padding: 0;max-width: 23px;}td.c13 {color: #232129;font-family: Arial, sans-serif;font-size: 16px;line-height: 22px;text-align: left;}td.c12 {font-size: 0pt;line-height: 0pt;text-align: left;}img.c11 {margin: 0;padding: 0;max-width: 20px;}div.c10 {color: #232129;font-family: Arial, sans-serif;font-size: 16px;line-height: 18px;text-align: center;}span.c9 {color: #1d78be;font-size: 20px;}table.c8 {font-size: 0pt;line-height: 0pt;text-align: center;width: 100%;min-width: 100%;}td.c7 {font-size: 0pt;line-height: 0pt;text-align: center;width: 100%;min-width: 100%;}div.c6 {color: #ff6f51;font-family: Arial, sans-serif;font-size: 20px;line-height: 26px;text-align: center;margin: 10px 0px 5px 0px;}a.c5 {color: #232129;text-decoration: none;}td.c4 {background-color: #DBDBDB;font-size: 0pt;line-height: 0pt;text-align: left;}div.c3 {font-size: 0pt;line-height: 0pt;text-align: left;}a.c2 {color: #232129;text-decoration: none;display: block;}img.c1 {margin: 0;padding: 0;width: 294px;height: 183px;}</style><body><table bgcolor="#ffffff" border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td align="center" valign="top"><table border="0" cellpadding="0" cellspacing="0" width="600"><tbody><tr><td class="td" style="font-size: 0pt; line-height: 0pt; padding: 0; margin: 0; font-weight: normal; width: 600px; min-width: 600px;" width="600"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td id="target"><div repeatindex="3"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="20"></td><td align="center"><table border="0" cellpadding="0" cellspacing="0" class="center"><tbody><tr><td><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="25"></td><td><div class="h6-center" style="color: #232129;font-size: 26px;line-height: 20px;text-align: center;padding-bottom: 10px;"><div>最新推荐房源</div></div><table border="0" cellpadding="0" cellspacing="0" class="spacer" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;" width="100%"><tbody><tr><td class="spacer" height="5" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;"> </td></tr></tbody></table></td><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="25"></td></tr></tbody></table><table bgcolor="#232129" border="0" cellpadding="0" cellspacing="0" class="border" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;" width="100%"><tbody><tr><td bgcolor="#232129" class="border" height="1" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;"> </td></tr></tbody></table></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" class="spacer" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;" width="100%"><tbody><tr><td class="spacer" height="10" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;"> </td></tr></tbody></table></td><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="20"></td></tr></tbody></table></div><div repeatindex="4"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="75"></td><td align="center"><div class="text-center" style="font-size: 20px;font-weight: normal;line-height: 30px;color: #333333;text-align: center;"><div><br/>我们为您推荐这些也许是您想要查看的最新房源。</div></div><table border="0" cellpadding="0" cellspacing="0" class="spacer" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;" width="100%"><tbody><tr><td class="spacer" height="35" style="font-size: 0pt; line-height: 0pt; text-align: center; width: 100%; min-width: 100%;"> </td></tr></tbody></table></td><td class="content-spacing" style="font-size: 0pt; line-height: 0pt; text-align: left;" width="75"></td></tr></tbody></table></div><!-- 房子 --><xsl:for-each select="./row"><div repeatindex="6"><table border="0" cellpadding="0" cellspacing="0" class="mobile-full" width="600"><tbody><tr><td colspan="3"><table border="0" cellpadding="0" cellspacing="0" style="margin-bottom:20px;" width="100%"><tbody><tr><!--奇数行--><th class="column-top c20" valign="top" width="294"><xsl:for-each select="./RecommendationsList[position() mod 2=1]"><xsl:call-template name="data2"></xsl:call-template></xsl:for-each></th><!--间隔--><th class="column-top" style="font-size:0pt;line-height:0pt;padding:0;margin:0;font-weight:normal;vertical-align:top;" valign="top" width="10"><table border="0" cellpadding="0" cellspacing="0" class="spacer c2" width="100%"><tbody><tr><td class="spacer c1" height="30"></td></tr></tbody></table></th><!--偶数行--><th class="column-top c20" valign="top" width="294"><xsl:for-each select="./RecommendationsList[position() mod 2=0]"><xsl:call-template name="data2"></xsl:call-template></xsl:for-each></th></tr></tbody></table></td></tr></tbody></table></div></xsl:for-each><!-- /房子 --></td></tr></tbody></table></td></tr></tbody></table></td></tr></tbody></table></body></html></xsl:template><xsl:template name="data2" match="child::*"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td><div class="fluid-img c3"><xsl:element name="a"><xsl:attribute name="href"><xsl:text>http://joannarealestate.com.cn/property.html?houseID=</xsl:text><xsl:value-of select="./ID"/></xsl:attribute><xsl:attribute name="class">c2</xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:element name="img"><xsl:attribute name="border">0</xsl:attribute><xsl:attribute name="width">294</xsl:attribute><xsl:attribute name="height">183</xsl:attribute><xsl:attribute name="class">c1</xsl:attribute><xsl:attribute name="href"><xsl:text>http://img.jrechina.com</xsl:text><xsl:value-of select="./PicUrl"/></xsl:attribute></xsl:element></xsl:element></div><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="img c4" width="1"></td><td><table style="border:1px #CCC solid;" border="0" cellpadding="0" cellspacing="0" class="c19" width="100%"><tbody><tr><td align="center" colspan="3"><div class="h4-1-orange-center c6" style="margin-top: 20px"><xsl:element name="a"><xsl:attribute name="href"><xsl:text>http://joannarealestate.com.cn/property.html?houseID=</xsl:text><xsl:value-of select="./ID"/></xsl:attribute><xsl:attribute name="class">c5</xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:attribute name="style">display: block;margin-bottom: 20px; font-size:16px</xsl:attribute><xsl:value-of select="./CommunityNameE"/></xsl:element></div><table border="0" cellpadding="0" cellspacing="0" class="spacer c8" width="100%"><tbody><tr><td class="spacer c7" height="5"></td></tr></tbody></table><div class="text-2-center c10" style="font-size:16px">¥<span class="c9"><xsl:value-of select="./Min_ZJ"/></span> /Month</div><table border="0" cellpadding="0" cellspacing="0" class="spacer c8" width="100%"><tbody><tr><td class="spacer c7" height="15"></td></tr></tbody></table><div><table border="0" cellpadding="0" cellspacing="0" class="center"><tbody><tr><td class="img c12" width="20"><img border="0" src="http://crm.joannarealestate.com.cn/HD/ZhongQiu_en/images/bed.jpg" class="c11"/></td><td class="img c12" width="5"></td><td class="text c13" style="font-size:16px"><div><xsl:value-of select="./Bedroom"/></div></td><td class="img c12" width="20"></td><td class="img c12" width="20"><img border="0" src="http://crm.joannarealestate.com.cn/HD/ZhongQiu_en/images/wash.jpg" class="c14"/></td><td class="img c12" width="5"></td><td class="text c13" style="font-size:16px"><div><xsl:value-of select="./Washroom"/></div></td><td class="img c12" width="20"></td><td class="img c12" width="20"><img border="0" src="http://crm.joannarealestate.com.cn/HD/ZhongQiu_en/images/sqm.jpg" class="c15"/></td><td class="img c12" width="5"></td><td class="text c13"><div style="font-size:16px"><xsl:value-of select="./RoomSize"/></div></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" class="spacer c8" width="100%"><tbody><tr><td class="spacer c7" height="15"></td></tr></tbody></table></div><table border="0" cellpadding="0" cellspacing="0" class="spacer c8" width="100%"><tbody><tr><td class="spacer c7" height="15"></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" class="center"><tbody><tr><td class="c18"><table border="0" cellpadding="0" cellspacing="0" width="100%"><tbody><tr><td class="text-btn-black c17" colspan="3" style="color:#232129;font-family:Arial, sans-serif;font-size:12px;width: 148px;height: 40px;line-height:16px;text-align:center;"><div><xsl:element name="a"><xsl:attribute name="href"><xsl:text>http://joannarealestate.com.cn/property.html?houseID=</xsl:text><xsl:value-of select="./RecommendationsList/ID"/></xsl:attribute><xsl:attribute name="class">link-black c16</xsl:attribute><xsl:attribute name="target">_blank</xsl:attribute><xsl:attribute name="style">color:#232129;text-decoration:none;</xsl:attribute><xsl:text>FIND OUT MORE</xsl:text></xsl:element></div></td></tr></tbody></table></td></tr></tbody></table><table border="0" cellpadding="0" cellspacing="0" class="spacer c8" width="100%"><tbody><tr><td class="spacer c7" height="30"></td></tr></tbody></table></td></tr></tbody></table></td><td class="img c4" width="1"></td></tr></tbody></table></td></tr></tbody></table></xsl:template>
</xsl:stylesheet>

xslt代码

5、调用接口生成xml的代码:

string Owner_ID = EncryptHelper.DESEncrypt("18745_9479", "test");
string url = string.Format("http://netapi.jrechina.com/Handler/FYGL.ashx?Action=GetRecommendationsList&lan={0}&Owner_ID={1}&page=1&rows=4", 0, Owner_ID);XDocument doc = new XDocument(new XProcessingInstruction("xml-stylesheet", "href=\"RecommendationsList.xslt\" type=\"text/xsl\""));
XElement eleRoot = new XElement("root");//根节点
doc.Add(eleRoot);//根据用户ID拼接请求获取该用户的推荐列表(取前4条)
string res = HttpHelper.GetData(url);
dynamic result = Utility.Helper.FromJson<dynamic>(res);if (result.DataList != null && (int)result.DataList.Count > 0)
{int totalCount = (int)result.DataList.Count;int rows = totalCount % 2 > 0 ? totalCount / 2 + 1 : totalCount / 2;//2个数据为一行XElement eleRow = new XElement("row");//eleRoot.Add(eleRow);
    XElement eleRecommendationsList;int index = 0;//循环到第几个foreach (dynamic item in result.DataList){index++;if (index % 2 == 1){//奇数个表示新起一行eleRow = new XElement("row");}eleRecommendationsList = new XElement("RecommendationsList",new XElement("ID", (item.ID??"").ToString()),new XElement("HouseNo", (item.HouseNo??"").ToString()),new XElement("RoomSize", (item.RoomSize??"").ToString()),new XElement("Bedroom", (item.Bedroom??"").ToString()),new XElement("Washroom", (item.Washroom??"").ToString()),new XElement("PicUrl", string.IsNullOrEmpty((string)item.PicUrl) ? "" : ((string)item.PicUrl).Replace("sm_", "").Replace("sf_", "").Replace(".", "@640@412@cut@1.")),new XElement("Min_ZJ", (item.Min_ZJ??"").ToString()),new XElement("FY_Subtitle", (item.FY_Subtitle??"").ToString()),new XElement("FY_Subtitle_CN", (item.FY_Subtitle_CN ?? "").ToString()),new XElement("CommunityNameE", (item.CommunityNameE ?? "").ToString()),new XElement("CommunityNameC", (item.CommunityNameC ?? "").ToString()),new XElement("PropertyName_C", (item.PropertyName_C ?? "").ToString()),new XElement("PropertyName_E", (item.PropertyName_E ?? "").ToString()),new XElement("E_Address", (item.E_Address ?? "").ToString()),new XElement("DistrictName", (item.DistrictName ?? "").ToString()),new XElement("DistrictNameC", (item.DistrictNameC ?? "").ToString()),new XElement("BusinessCircleName", (item.BusinessCircleName ?? "").ToString()),new XElement("BusinessCircleNameC", (item.BusinessCircleNameC ?? "").ToString()));eleRow.Add(eleRecommendationsList);if (index % 2 == 0|| index % 2 > 0&& index >= totalCount){//是偶数个或最后一个且是奇数个
            eleRoot.Add(eleRow);}}
}string savePath = @"G:\zhyue\backup\projects\Test\ConsoleApplication1\测试\XSLT和XML结合使用\RecommendationsList.xml";
if (File.Exists(savePath)) File.Delete(savePath);
doc.Save(savePath);

调用接口生成xml代码

6、浏览器中查看xml:

成功,不过xml无法直接访问图片,没找到解决法案,html是可以的

转载于:https://www.cnblogs.com/zhyue93/p/xml.html

C# xml通过xslt转换为html输出相关推荐

  1. c#中使用XSLT将xml文档转换为html文档方法

    c#使用XSLT将xml文档转换为html文档 使用xslt可以将xml文档转成html,如下c#函数: 需要引用下面的命名空间: using System.Xml; using System.Xml ...

  2. c# 修改xslt并转为html,c#使用XSLT将xml文档转换为html文档

    c#使用XSLT将xml文档转换为html文档 使用xslt可以将xml文档转成html,如下c#函数: 需要引用下面的命名空间: using System.Xml; using System.Xml ...

  3. 使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页

    在Web开发中,我们经常会遇到分页显示和排序数据记录集的情况,这在服务器端使用服务器端的代码和数据库技术是件很轻松的事情,比如:ASP.PHP.JSP等.然而,如果要在客户端显示多条记录并且排序是一件 ...

  4. (转)使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页

    在Web开发中,我们经常会遇到分页显示和排序数据记录集的情况,这在服务器端使用服务器端的代码和数据库技术是件很轻松的事情,比如:ASP.PHP.JSP等.然而,如果要在客户端显示多条记录并且排序是一件 ...

  5. 使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页面[转载]

    在Web开发中,我们经常会遇到分页显示和排序数据记录集的情况,这在服务器端使用服务器端的代码和数据库技术是件很轻松的事情,比如:ASP.PHP.JSP等.然而,如果要在客户端显示多条记录并且排序是一件 ...

  6. 使用XML、XSLT和XPath创建可排序、分页、重用的数据显示页面

    使用XML.XSLT和XPath创建可排序.分页.重用的数据显示页面 在Web开发中,我们经常会遇到分页显示和排序数据记录集的情况,这在服务器端使用服务器端的代码和数据库技术是件很轻松的事情,比如:A ...

  7. DCMTK:将XML文档转换为DICOM文件或数据集

    DCMTK:将XML文档转换为DICOM文件或数据集 将XML文档转换为DICOM文件或数据集 将XML文档转换为DICOM文件或数据集 #include "dcmtk/config/osc ...

  8. moxy json介绍_MOXy的对象图– XML和JSON的输入/输出局部模型

    moxy json介绍 假设您有一个要公开为RESTful服务的域模型. 问题是您只想输入/输出部分数据. 以前,您将创建一个代表子集的单独模型,然后使用代码在模型之间移动数据. 在EclipseLi ...

  9. MOXy的对象图– XML和JSON的输入/输出局部模型

    假设您有一个要公开为RESTful服务的域模型. 问题是您只想输入/输出部分数据. 以前,您将创建一个代表子集的单独模型,然后使用代码在模型之间移动数据. 在EclipseLink 2.5.0中,我们 ...

最新文章

  1. N76E003介绍与开发资料获取
  2. Linux基础第四课——文件操作
  3. 恶意软件伪装“正规军”,撕开Booster Cleaner“画皮”下的真相
  4. 使用IPFS集群搭建创建私有IPFS网络
  5. 0829双层循环嵌套
  6. MySQL高可用方案-PXC环境部署记录
  7. gitolite安装及配置
  8. android应用推荐
  9. python窗口中导入图片_Python3 tkinter基础 Text image 文本框中插入图片
  10. 三大突破让人工智能终成现实
  11. 更改MySQL密码并验证,及使用SQLyog登入MySQ
  12. 接入高德sdk的几个问题,=。=
  13. 将字符串中的大写字母转化为小写字母
  14. 谷歌浏览器恐龙游戏开挂秘诀
  15. 友点 CMS V9.1 后台登录绕过 GetShell
  16. Windows - 两台电脑共享磁盘 / 映射网络驱动器
  17. 【03yy and one】
  18. 跟踪`极点`五笔变半行.顺便学习Process Monitor的使用
  19. KMP算法:HDU-2087-剪花布条
  20. 长篇连载(一):你的编程能力从什么时候开始突飞猛进?

热门文章

  1. laravel框架路由设置404方式
  2. 解决NGUI中sprite的边缘会出现黑线的问题
  3. rtmp的URL里面mp3:和mp4:是啥意思
  4. 内核配置中 ramdisk 大小修改
  5. SharePoint 2007部署过程详细图解之一:准备
  6. ie关闭浏览器tab提示信息
  7. [笔记]何为Linux及其文件系统(四)
  8. oracle中导入dmp数据注意事项
  9. CRichEditCtrl 好象没有复制、粘贴的功能(Ctrl+C、Ctrl+v都不好用
  10. 苹果Swift语言中文教程资源汇总