我正在使用SDL Tridion 2011 SP1中的Tom.Net API。 我想檢索XhtmlField的「源」部分。在SDL中使用Tom.Net API獲取Xhtml字段的完整XML源Tridion 2011 SP1
我的源代碼看起來像這樣。
<Content>
<text>
<p xmlns="http://www.w3.org/1999/xhtml">hello all<strong>
<a id="ID1" href="#" name="ZZZ">Name</a>
</strong></p>
</text>
</Content>
我想獲取此「文本」字段的來源並處理名稱爲a
的標籤。
我嘗試以下操作:
ItemFields content = new ItemFields(sourcecomp.Content, sourcecomp.Schema);
XhtmlField textValuesss = (XhtmlField)content["text"];
XmlElement textxmlelement = textValuesss.Definition.ExtensionXml;
Response.Write("<BR>" + "count:" + textxmlelement.ChildNodes.Count);
for (int i = 0; i < textxmlelement.ChildNodes.Count; i++)
{
Response.Write("<BR>" + "nodes" + textxmlelement.ChildNodes[i].Name);
}
//get all the nodes with the name a
XmlNodeList nodeswithnameA = textxmlelement.GetElementsByTagName("a");
foreach (XmlNode eachNode in nodeswithnameA)
{
//get the value at the attribute "id" of node "a"
string value = eachNode.Attributes["id"].Value;
Response.Write("<BR>" + "idValue" + value);
}
我沒有得到任何輸出。更重要的是,我將Count算作Zero。
輸出我:
數:0
雖然我已經在該領域的一些子標籤,我沒有得到爲什麼0即將爲Count
。
任何可以提出需要的修改。
謝謝。
他尋找多個錨定,所以.SelectNodes()會更好 –
的確。重點是使用XPath而不是循環子節點。 –