簡體   English   中英

選擇節點LINQ XML的HTML內容

[英]Selecting HTML Content of a node LINQ XML

我有這樣的XML

  <component>
        <section>
          <title>Reporting Parameters</title>
          <text>
            <list>
              <item>Reporting period: January 1st, 2012
            </list>
          </text>
       </section>
   </component>

我想選擇包括等元素的節點的全部內容,但只選擇純文本“報告期:2012年1月1日”,原因是它可能包含一些我需要存儲在數據庫中的HTML標記,我正在使用以下查詢

  var components = (from c in cdafile.Root.Elements(ns + "component")
                  select new{
                      name = (string)c.Element(ns + "section").Element(ns + "title").Value,
                      text = (string)c.Element(ns + "section").Element(ns + "text"),
                  });

XElement重載了顯式強制轉換運算符( 以返回級聯的節點內部文本 ),請使用.ToString()獲取節點內容:

text = c.Element(ns + "section").Element(ns + "text").ToString()

讓孩子加價:

text = string.Join(Environment.NewLine, c
    .Element(ns + "section").Element(ns + "text")
    .Elements().Select(e => e.ToString())
)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM