pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

  • <i id='01XiL'><tr id='01XiL'><dt id='01XiL'><q id='01XiL'><span id='01XiL'><b id='01XiL'><form id='01XiL'><ins id='01XiL'></ins><ul id='01XiL'></ul><sub id='01XiL'></sub></form><legend id='01XiL'></legend><bdo id='01XiL'><pre id='01XiL'><center id='01XiL'></center></pre></bdo></b><th id='01XiL'></th></span></q></dt></tr></i><div class="gogc2ma" id='01XiL'><tfoot id='01XiL'></tfoot><dl id='01XiL'><fieldset id='01XiL'></fieldset></dl></div>

  • <small id='01XiL'></small><noframes id='01XiL'>

    <tfoot id='01XiL'></tfoot>
        • <bdo id='01XiL'></bdo><ul id='01XiL'></ul>
        <legend id='01XiL'><style id='01XiL'><dir id='01XiL'><q id='01XiL'></q></dir></style></legend>

        在 C# 中使用 Html 敏捷性解析表格、單元格

        Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)

              <bdo id='F4Drs'></bdo><ul id='F4Drs'></ul>

              <legend id='F4Drs'><style id='F4Drs'><dir id='F4Drs'><q id='F4Drs'></q></dir></style></legend>
                <tbody id='F4Drs'></tbody>

                1. <tfoot id='F4Drs'></tfoot>

                  <small id='F4Drs'></small><noframes id='F4Drs'>

                2. <i id='F4Drs'><tr id='F4Drs'><dt id='F4Drs'><q id='F4Drs'><span id='F4Drs'><b id='F4Drs'><form id='F4Drs'><ins id='F4Drs'></ins><ul id='F4Drs'></ul><sub id='F4Drs'></sub></form><legend id='F4Drs'></legend><bdo id='F4Drs'><pre id='F4Drs'><center id='F4Drs'></center></pre></bdo></b><th id='F4Drs'></th></span></q></dt></tr></i><div class="ausgkgo" id='F4Drs'><tfoot id='F4Drs'></tfoot><dl id='F4Drs'><fieldset id='F4Drs'></fieldset></dl></div>

                  本文介紹了在 C# 中使用 Html 敏捷性解析表格、單元格的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我需要解析 Html 代碼.更具體地說,解析所有表中每一行的每個單元格.每行代表一個對象,每個單元格代表不同的屬性.我想解析這些以便能夠編寫一個包含每個數據的 XML 文件(沒有無用的 HTML 代碼).我已經成功地解析了 HTML 文件中的每一列,但現在我不知道將其寫入 XML 文件的選項是什么.我很困惑.

                  I need to parse Html code. More specifically, parse each cell of every rows in all tables. Each row represent a single object and each cell represent different properties. I want to parse these to be able to write an XML file with every data inside (without the useless HTML code). I have successfully been able to parse each column from the HTML file but now I don't know what my options are for writing this to an XML file. I am baffled.

                  HTML:

                  <tr><tr> 
                  <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF"> 
                      1
                  </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="left"> 
                          <a href="/ice/player.htm?id=8471675">Sidney Crosby</a> 
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="center"> 
                          PIT
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="center"> 
                          C
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          39
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          32
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          33
                      </td> 
                      <td class="statBox sorted" style="border-width:0px 1px 1px 0px; background-color: #E0E0E0" align="right"> 
                          <font color="#000000"> 
                              65
                          </font> 
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          20
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          29
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          10
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          1
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          3
                      </td> 
                      <td class="statBox" style="border-width:0px 0px 1px 0px; background-color: #FFFFFF" align="right"> 
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          0
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          154
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          20.8
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          21:54
                      </td> 
                      <td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"> 
                          22.6
                      </td> 
                      <td class="statBox" style="border-width:0px 0px 1px 0px; background-color: #FFFFFF" align="right"> 
                          55.7
                      </td> 
                  </tr></tr>
                  

                  C#:

                  using HtmlAgilityPack;
                  
                  namespace Stats
                  {
                      class StatsParser
                      {
                          private string htmlCode;
                          private static string fileName = "[" + DateTime.Now.ToShortDateString() + " NHL Stats].xml";
                  
                          public StatsParser(string htmlCode)
                          {
                              this.htmlCode = htmlCode;
                              this.ParseHtml();
                          }
                  
                          public void ParseHtml()
                      {
                          HtmlDocument doc = new HtmlDocument();
                          doc.LoadHtml(htmlCode);
                  
                          try
                          {
                              // Get all tables in the document
                              HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("http://table");
                  
                              // Iterate all rows in the first table
                              HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
                              for (int i = 0; i < rows.Count; ++i)
                              {
                  
                                  // Iterate all columns in this row
                                  HtmlNodeCollection cols = rows[i].SelectNodes(".//td[@class='statBox']");
                                  for (int j = 0; j < cols.Count; ++j)
                                  {
                  
                                      // Get the value of the column and print it
                                      string value = cols[j].InnerText;
                                      if (value!="")
                                          System.Windows.MessageBox.Show(value);
                                  }
                              }
                          }
                          catch (NullReferenceException)
                          {
                              System.Windows.Forms.MessageBox.Show("Exception!!");
                          }
                      }
                  

                  XML:

                  <?xml version="1.0" encoding="utf-8" ?>
                  
                  <Stats Date="2011-01-01">
                    <Player Rank="1">
                      <Name>Sidney Crosby</Name>
                      <Team>PIT</Team>
                      <Position>C</Position>
                      <GamesPlayed>39</GamesPlayed>
                      <Goals>32</Goals>
                      <Assists>33</Assists>
                    </Player>
                  </Stats>
                  

                  推薦答案

                  看了一圈MSDN,終于找到了解決自己問題的實現方案:

                  After looking around MSDN, I finally found an implementation solution to my problem:

                      using System;
                      using HtmlAgilityPack;
                      using System.Xml;
                  
                      namespace HockeyStats
                      {
                          class StatsParser
                          {
                              private string htmlCode;
                              private static string fileName = "[" + DateTime.Now.ToShortDateString() + " NHL Stats].xml";
                  
                              public StatsParser(string htmlCode)
                              {
                                  this.htmlCode = htmlCode;
                  
                                  this.ParseHtml();
                              }
                  
                              public void ParseHtml()
                              {
                  
                                  HtmlDocument doc = new HtmlDocument();
                                  doc.LoadHtml(htmlCode);
                                  XmlWriter writer = null;
                  
                                  try
                                  {
                                      // Create an XmlWriterSettings object with the correct options. 
                                      XmlWriterSettings settings = new XmlWriterSettings();
                                      settings.Indent = true;
                                      settings.IndentChars = ("  ");
                                      settings.OmitXmlDeclaration = false;
                  
                                      // Create the XmlWriter object and write some content.
                                      writer = XmlWriter.Create(@"...."+fileName, settings);
                                      writer.WriteStartElement("Stats");
                                      writer.WriteAttributeString("Date", DateTime.Now.ToShortDateString());
                  
                                  // Iterate all rows within another row
                                  HtmlNodeCollection rows = doc.DocumentNode.SelectNodes(".//tr/tr");
                                  for (int i = 0; i < rows.Count; ++i)
                                  {
                                      // Iterate all columns in this row
                                      HtmlNodeCollection cols = rows[i].SelectNodes(".//td[@class='statBox']");
                                      for (int j = 0; j < 20; ++j)
                                      {
                                                  switch (j)
                                                  {
                                                      case 0:
                                                          {
                                                              writer.WriteStartElement("Player");
                                                              writer.WriteAttributeString("Rank", cols[j].InnerText.Trim()); break;
                                                          }
                                                      case 1: writer.WriteElementString("Name", cols[j].InnerText.Trim()); break;
                                                      case 2: writer.WriteElementString("Team", cols[j].InnerText.Trim()); break;
                                                      case 3: writer.WriteElementString("Pos", cols[j].InnerText.Trim()); break;
                                                      case 4: writer.WriteElementString("GP", cols[j].InnerText.Trim()); break;
                                                      case 5: writer.WriteElementString("G", cols[j].InnerText.Trim()); break;
                                                      case 6: writer.WriteElementString("A", cols[j].InnerText.Trim()); break;
                                                      case 7: writer.WriteElementString("PlusMinus", cols[j].InnerText.Trim()); break;
                                                      case 8: writer.WriteElementString("PIM", cols[j].InnerText); break;
                                                      case 9: writer.WriteElementString("PP", cols[j].InnerText); break;
                                                      case 10: writer.WriteElementString("SH", cols[j].InnerText); break;
                                                      case 11: writer.WriteElementString("GW", cols[j].InnerText); break;
                                                      case 12: writer.WriteElementString("OT", cols[j].InnerText); break;
                                                      case 13: writer.WriteElementString("Shots", cols[j].InnerText); break;
                                                      case 14: writer.WriteElementString("ShotPctg", cols[j].InnerText); break;
                                                      case 15: writer.WriteElementString("TOIPerGame", cols[j].InnerText); break;
                                                      case 16: writer.WriteElementString("ShiftsPerGame", cols[j].InnerText); break;
                                                      case 17: writer.WriteElementString("FOWinPctg", cols[j].InnerText); break;
                  
                                                  }
                                              }
                                          }
                                          writer.WriteEndElement();
                                      }
                                      writer.WriteEndElement();
                                      writer.Flush();
                                  }
                                  finally
                                  {
                                      if (writer != null)
                                          writer.Close();
                                  }
                              }
                          }
                      }
                  

                  提供以下 XML 文件作為輸出:

                  which gives the following XML file as an output:

                  <?xml version="1.0" encoding="utf-8" ?> 
                  <Stats Date="2011-01-01">
                   <Player Rank="1">
                    <Name>Sidney Crosby</Name> 
                    <Team>PIT</Team> 
                    <Pos>C</Pos> 
                    <GP>39</GP> 
                    <G>32</G> 
                    <A>33</A> 
                    <PlusMinus>20</PlusMinus> 
                    <PIM>29</PIM> 
                    <PP>10</PP> 
                    <SH>1</SH> 
                    <GW>3</GW> 
                    <Shots>0</Shots> 
                    <ShotPctg>154</ShotPctg> 
                    <TOIPerGame>20.8</TOIPerGame> 
                    <ShiftsPerGame>21:54</ShiftsPerGame> 
                    <FOWinPctg>22.6</FOWinPctg> 
                   </Player>
                  </Stats>
                  

                  這篇關于在 C# 中使用 Html 敏捷性解析表格、單元格的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                  XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                  Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                  delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                  Parse malformed XML(解析格式錯誤的 XML)
                  extracting just page text using HTMLAgilityPack(使用 HTMLAgilityPack 僅提取頁面文本)
                  <i id='tkz0P'><tr id='tkz0P'><dt id='tkz0P'><q id='tkz0P'><span id='tkz0P'><b id='tkz0P'><form id='tkz0P'><ins id='tkz0P'></ins><ul id='tkz0P'></ul><sub id='tkz0P'></sub></form><legend id='tkz0P'></legend><bdo id='tkz0P'><pre id='tkz0P'><center id='tkz0P'></center></pre></bdo></b><th id='tkz0P'></th></span></q></dt></tr></i><div class="0cmyqq2" id='tkz0P'><tfoot id='tkz0P'></tfoot><dl id='tkz0P'><fieldset id='tkz0P'></fieldset></dl></div>

                  <legend id='tkz0P'><style id='tkz0P'><dir id='tkz0P'><q id='tkz0P'></q></dir></style></legend>

                        • <bdo id='tkz0P'></bdo><ul id='tkz0P'></ul>
                            <tbody id='tkz0P'></tbody>

                            <small id='tkz0P'></small><noframes id='tkz0P'>

                            <tfoot id='tkz0P'></tfoot>
                            主站蜘蛛池模板: 订做不锈钢_不锈钢定做加工厂_不锈钢非标定制-重庆侨峰金属加工厂 | 洛阳防爆合格证办理-洛阳防爆认证机构-洛阳申请国家防爆合格证-洛阳本安防爆认证代办-洛阳沪南抚防爆电气技术服务有限公司 | 注塑模具_塑料模具_塑胶模具_范仕达【官网】_东莞模具设计与制造加工厂家 | 碳钢法兰厂家,非标法兰,定制异型,法兰生产厂家-河北九瑞管道 | 高压贴片电容|贴片安规电容|三端滤波器|风华电容代理南京南山 | 铝合金重力铸造_铝合金翻砂铸造_铝铸件厂家-东莞市铝得旺五金制品有限公司 | 贝壳粉涂料-内墙腻子-外墙腻子-山东巨野七彩贝壳漆业中心 | 企业管理培训,企业培训公开课,企业内训课程,企业培训师 - 名课堂企业管理培训网 | 游泳池设计|设备|配件|药品|吸污机-东莞市太平洋康体设施有限公司 | 岛津二手液相色谱仪,岛津10A液相,安捷伦二手液相,安捷伦1100液相-杭州森尼欧科学仪器有限公司 | 万濠投影仪_瑞士TRIMOS高度仪_尼康投影仪V12BDC|量子仪器 | 西门子代理商_西门子变频器总代理-翰粤百科 | 电表箱-浙江迈峰电力设备有限公司-电表箱专业制造商 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 成都中天自动化控制技术有限公司| 旋振筛_不锈钢旋振筛_气旋筛_旋振筛厂家—新乡市大汉振动机械有限公司 | 刘秘书_你身边专业的工作范文写作小秘书 | 中高频感应加热设备|高频淬火设备|超音频感应加热电源|不锈钢管光亮退火机|真空管烤消设备 - 郑州蓝硕工业炉设备有限公司 | 武汉刮刮奖_刮刮卡印刷厂_为企业提供门票印刷_武汉合格证印刷_现金劵代金券印刷制作 - 武汉泽雅印刷有限公司 | 「安徽双凯」自动售货机-无人售货机-成人用品-自动饮料食品零食售货机 | 温湿度记录纸_圆盘_横河记录纸|霍尼韦尔记录仪-广州汤米斯机电设备有限公司 | 手板-手板模型-手板厂-手板加工-生产厂家,[东莞创域模型] | 真空泵厂家_真空泵机组_水环泵_旋片泵_罗茨泵_耐腐蚀防爆_中德制泵 | 黑田精工电磁阀-CAMMOZI气缸-ROSS电磁-上海茂硕机械设备有限公司 | 折弯机-刨槽机-数控折弯机-数控刨槽机-数控折弯机厂家-深圳豐科机械有限公司 | CE认证_产品欧盟ROHS-REACH检测机构-商通检测 | 浙江富广阀门有限公司| PCB设计,PCB抄板,电路板打样,PCBA加工-深圳市宏力捷电子有限公司 | 水环真空泵厂家,2bv真空泵,2be真空泵-淄博真空设备厂 | 杭州厂房降温,车间降温设备,车间通风降温,厂房降温方案,杭州嘉友实业爽风品牌 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 没斑啦-专业的祛斑美白嫩肤知识网站-去斑经验分享 | 河南凯邦机械制造有限公司 | 防伪溯源|防窜货|微信二维码营销|兆信_行业内领先的防伪防窜货数字化营销解决方案供应商 | 双吸泵,双吸泵厂家,OS双吸泵-山东博二泵业有限公司 | 贴片电容代理-三星电容-村田电容-风华电容-国巨电容-深圳市昂洋科技有限公司 | 硬度计_影像测量仪_维氏硬度计_佛山市精测计量仪器设备有限公司厂家 | 旗杆生产厂家_不锈钢锥形旗杆价格_铝合金电动旗杆-上海锥升金属科技有限公司 | 单螺旋速冻机-双螺旋-流态化-隧道式-食品速冻机厂家-广州冰泉制冷 | 合肥废气治理设备_安徽除尘设备_工业废气处理设备厂家-盈凯环保 合肥防火门窗/隔断_合肥防火卷帘门厂家_安徽耐火窗_良万消防设备有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 |