我正在使用SDL Tridion 2011 SP1中的Tom.Net API.我试图检索XhtmlField的"源"部分.
我的来源看起来像这样.
<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>
Run Code Online (Sandbox Code Playgroud)
我想获取此"text"字段的来源并使用名称处理标记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);
}
Run Code Online (Sandbox Code Playgroud)
我没有得到任何输出.更重要的是,我的计数为零.
我得到的输出:
数:0
虽然我在这个领域有一些儿童标签,但我不知道0为什么会出现Count.
可以建议所需的修改.
谢谢.
ItemField.Definition允许访问字段的Schema Definition,而不是字段内容,因此您不应使用ExtensionXml属性来访问内容(这就是为什么它是空的).此属性用于在架构定义中存储扩展数据.
要使用包含XML/XHTML内容的字段,我只需访问组件的Content属性,因为它已经是XmlElement.您需要注意内容的命名空间,因此在查询此XmlElement时请使用XmlNamespaceManager.例如,以下内容将为您提供名为"text"的字段的引用:
XmlNameTable nameTable = new NameTable();
XmlNamespaceManager nsManager = new XmlNamespaceManager(nameTable);
nsManager.AddNamespace("custom", sourceComp.Content.NamespaceURI);
XmlElement fieldValue = (XmlElement)sourceComp.Content.SelectSingleNode(
"/custom:Content/custom:text", nsManager);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
208 次 |
| 最近记录: |