user1421558
user1421558

Reputation: 1

generating xml containg fields of a component in sdl tridion

How to generate xml file which contain all the fields of a component in SDl Tridion? I have to do this by writing C# code. I am using the following code:

 public class stockcompany : ITemplate
    {
        private Engine engine;
        private Package package;
        public void Transform(Engine engine, Package package)

        {

            this.engine = engine;
            this.package = package;



            StringBuilder sb = new StringBuilder();
            Component component = engine.GetObject(package.GetValue("Component.ID")) as Component;
            ItemFields componentfields = new ItemFields(component.Content, component.Schema);
            ItemFields compometa = new ItemFields(component.Metadata, component.Schema);



            if (compometa.Contains("check"))
           {

           }

            if (componentfields.Contains("companyname"))
            {

                 string company = string.Empty;

                company = componentfields["companyname"].ToString();
                package.PushItem("xml", package.CreateHtmlItem(company.ToString()));


         XDocument myxml = new XDocument (new XDeclaration ("1.0","UTF-8",null ),new XElement ("companies",new XElement ("company",xml)));

            }

Upvotes: 0

Views: 769

Answers (4)

Alvin Reyes
Alvin Reyes

Reputation: 2887

I've typically resorted to XSLT to get source XML via a component template, but we can definitely do the same with C#.

[TcmTemplateTitle("Show XML Guts")]
public class ShowXmlGuts : ITemplate
  {
  public void Transform(Engine engine, Package package)
    {
    Item contentItem = package.GetByType(ContentType.Component);
    Component component = engine.GetObject(contentItem.GetAsSource().GetValue("ID")) as Component;
    package.PushItem("componentSource", package.CreateHtmlItem(component.Content.OuterXml));
    }
  }

Pick-and-Choose Fields

If at all possible, I'd start with an intermediate XML format that's not a one-to-one mapping to component source.

We're meant to get and transform fields with the appropriate APIs. Relying on the source component format could be problematic in your next major change, which could include:

  • schema changes and new keywords
  • presentation/rendering side or CMS changes
  • "unexpected" content (rich text, special characters, tcm references, etc)

Alternatives to C#

In terms of technology:

  • XSLT is great for "XML generation," even if done in a C# template building block.
  • The XSLT Mediator would a good choice, if allowed in your environment
  • You could create XML with DWT, simplifying field selection; however, it's easier to create invalid XML this way (XSLT doesn't validate your XML either, but it's slightly harder to break node nesting)

If possible, update your question with the output XML you're trying to achieve or start a new question to get from your raw component XML to desired output.

Upvotes: 2

Quirijn
Quirijn

Reputation: 3547

Another approach to generating XML is to use an XmlSerializer. It works as follows:

  • Create a POCO
  • Fill it with values from the component
  • Use XmlSerializer to deserialize it into an XML string
  • Store the XML in the Output variable of the package

This is how DD4T does it. As Nuno said, it is worth while to check it out (see his answer).

Upvotes: 3

Nuno Linhares
Nuno Linhares

Reputation: 10234

You are probably re-inventing the wheel here.

  1. Tridion items are XML. The .Content and .Metadata properties of a component return XML, would this be enough?
  2. DD4T uses templates that publish XML to the delivery side, might be worth checking it: http://code.google.com/p/dynamic-delivery-4-tridion/

Given that DD4T is open sourced, you may want to check how they do it as an example.

Upvotes: 5

johnwinter
johnwinter

Reputation: 3624

Your code isn't very clear and contains a lot of problems, but generally you seem to be on the correct track.

1) You need to cast your

componentfields["companyname"].ToString();

I suspect you're working with a TextField here so cast it to a TextField object and then use the objects .value property

2) Here is where you push the value into the package, this will contain whatever you got from your 'companyname' field, it could be xml, it may not be:

package.PushItem("xml", package.CreateHtmlItem(company.ToString()));

..But I think with this information you can find your way to what you need.

Good luck!

Upvotes: 1

Related Questions