I am creating a C# TBB. I have the XML code as shown below.
<content>
<ah>123</ah>
<ph>456</ph>
<body>
<sc>hi</sc>
<value>aa</value>
<value>bb</value>
<value>cc</value>
<value>dd</value>
<value>dd</value>
</body>
<body>
<sc>hello</sc>
<value>ee</value>
<value>ddff</value>
</body>
</content>
C# TBB code:
using (MemoryStream ms = new MemoryStream())
{
XmlTextWriter securboxXmlWriter = new XmlTextWriter(ms, new System.Text.UTF8Encoding(false));
securboxXmlWriter.Indentation = 4;
securboxXmlWriter.Formatting = Formatting.Indented;
securboxXmlWriter.WriteStartDocument();
securboxXmlWriter.WriteStartElement("component");
securboxXmlWriter.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
securboxXmlWriter.WriteAttributeString("xmlns", "http://www.w3.org/1999/xhtml");
securboxXmlWriter.WriteStartElement("content");
securboxXmlWriter.WriteStartElement("wire:wire");
securboxXmlWriter.WriteStartElement("wire:si");
securboxXmlWriter.WriteStartElement("wg:ah");
securboxXmlWriter.WriteElementString("text", package.GetValue("Component.ah"));
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteStartElement("wg:ph");
securboxXmlWriter.WriteElementString("nlt", package.GetValue("Component.ph"));
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndElement();
securboxXmlWriter.WriteEndDocument();
securboxXmlWriter.Flush();
securboxXmlWriter.Close();
Item output = package.GetByName("Output");
if (output != null)
{
package.Remove(output);
}
package.PushItem("Output", package.CreateStringItem(ContentType.Xml, Encoding.UTF8.GetString(ms.ToArray())));
}
In the XML code "body" tag is occured multiple times. I need to extract the each and every "body" tag content. For that purpose I am using HTML agility pack. To make it work in the C# TBB, How to add the HTML agility pack DLL to the Tridion system? And also please provide a sample code snippet in html agility to loop through the body tags.
If HTML Agility is not going to work with C# TBB then please suggest me a way how to get the "body" tag content?
Thanks in advance.Early response is appreciated.
You will need to place the third party DLL in the Global Assembly Cache (GAC). For the sample agility pack code I suggest you ask a separate question for that with a more specific title.
Using ILMerge saves you the hassle of installing external DLLs in the GAC. Just make sure you do not break any (copyright) laws by merging your code with someone else's. Technically, it is simple enough to do: after your template DLL has been built, a post-build action merges it with the external DLLs you may need, and creates a new DLL. You can then upload that merged DLL to Tridion.
Example: let's say your template project is called 'MyTemplates' and it has a dependency on ExternalLibraryOne.dll and ExternalLibraryTwo.dll. Here's what you do:
Open the properties of your templating project and add the following post-build event:
"$(SolutionDir)Solution Items\ilmerge"
/lib:"C:\Windows\Microsoft.NET\Framework\v4.0.30319"
/t:dll /targetplatform:v4,C:\Windows\Microsoft.NET\Framework\v4.0.30319
/out:"$(TargetDir)$(ProjectName).merged.dll"
"$(TargetDir)ExternalLibraryOne.dll"
"$(TargetDir)ExternalLibraryTwo.dll"
"$(TargetPath)"
Build your template project. The output folder (bin/Debug or bin/Release) will now contain a file called MyTemplates.merged.dll. You can upload this to Tridion using TcmUploadAssembly.
This is assuming you are using .NET 4 of course.
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