I am working on Tom.Net APIs in SDL Tridion 2011 SP1. I am trying to retrieve the "source" part of the XhtmlField.
My source look like this.
<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>
I want to get the source of this "text" field and process the tags with name a
.
I tried following:
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);
}
I am not getting any output. More over I am getting the count as Zero.
Output I got:
count:0
Though I have some child tags in the field, I am not getting why 0 is coming as Count
.
Can any suggest the modification needed.
Thank you.
ItemField.Definition gives access to the Schema Definition of the field, and not the field content, so you should not use the ExtensionXml property to access content (thats why its empty). This property is used for storing extension data in the schema definition.
To work with fields containing XML/XHTML content I would just access the Content property of the component as this is already an XmlElement. You need to be careful about the namespace of the content, so use an XmlNamespaceManager when querying this XmlElement. For example the following will give you a reference to the field named '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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With