4

I want to create a component by giving XML source input directly using core service 2011, in SDL Tridion 2011.

I want to write code to create a component by uploading source XML. Using the core service 2011.

Say name of the xml file is helloworld.xml and location is D:\abcd\cdef\all\helloworld.xml.

I have written the code like this, but its not working.

XmlDocument contentxml = new XmlDocument();
contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
Response.Write("<BR>" + contentxml.ToString());
component.Content = contentxml.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

The Response.write is displaying nothing. Correct me if I missed any thing. It's not creating any component and error is coming.

When i tried this:

XmlDocument contentxml = new XmlDocument();
try
{
    contentxml.LoadXml(@"D:\abcd\cdef\all\helloworld.xml");
}
catch (XmlException exp)
{
    Console.WriteLine(exp.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
contentxml.WriteTo(xw);
Response.Write("<BR>" + sw.ToString());

component.Content = sw.ToString();
ComponentData comp = (ComponentData)client.Create(component, new ReadOptions());

This time it's showing unable to find UUId: some thing like that.

My helloworld.xml looks like this.

<Content xmlns="uuid:1111eb85-0X11-11f9-1e2X-1X82X78fX920">
    <first>Hello World.This is Fisrt field</first>
    <second>Hello World.This is second field</second>
</Content>

It would be great if some one share some sample code to do it.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Patan
  • 17,073
  • 36
  • 124
  • 198
  • Please state what you have tried so far - preferably with the code that is not working for you. As it stands, you seem to expect people to write the code for you. – Peter Kjaer Mar 30 '12 at 10:18
  • 3
    This is hardly Tridion-related, to be honest... Not being able to load an XML Document using System.Xml classes has nothing to do with CoreService or Tridion... – Nuno Linhares Mar 30 '12 at 13:15

3 Answers3

8

Loading any XML from a a file and trying to create a component won't work unless the XML uses the format the CMS is expecting.

The XML structure of a component in SDL Tridion has some fixed parts (Example Nodes Content, Metadata) plus some flexible parts (The way you define the fields). First you need to have the XML with the same structure that the CMS is expecting. Typically the nodes that should be in your xml are the CONTENT and METADATA, load those in an XML Document and use the Core Service API to create a component using the content contained in those nodes. The best way to know how which is the structure of a component based on an schema is to create a sample component using the Tridion UI and see how the XML is constructed. After that you need to create your XML Sources using that structure. I posted recently an example of how to create a component using the Core Service, please have a look at that.

Faulted State error while creating component with Core Service

Following this code, you can access the nodes Content and Metadata

componentData.Content = xmlUtil.GetNewXmlNode("Content", schemaData.NamespaceUri); componentData.Metadata = xmlUtil.GetNewXmlNode("Metadata", schemaData.NamespaceUri);

And replace those with your content

Community
  • 1
  • 1
Miguel
  • 711
  • 3
  • 4
6

The general outline:

  1. Load the XML from the file into an XDocument / XmlDocument.
  2. Create a new Component by calling GetDefaultData on the client.
  3. Set the Content property of the Component to the XML.
  4. Save the Component by calling Save on the client.

If you haven't already, please have a look at the Core Service API documentation available on SDL Tridion World.

If you have trouble implementing this, please post the code that you have and what you have tried in order to make it work.

Peter Kjaer
  • 4,316
  • 13
  • 23
  • I have edited the question indicating part that i tried, please have alook at it. – Patan Mar 30 '12 at 12:27
  • 2
    I believe you need to use .Load instead of .LoadXML when you want it to load from a file. Otherwise it expects you to pass in the XML as-is. – Peter Kjaer Mar 30 '12 at 13:06
4

Using XmlDocument.LoadXML() expects an XML string as input, as commented by Peter you should use XMLDocument.Load() instead, see here for more details http://msdn.microsoft.com/en-us/library/a8ta6tz4.aspx

When you have passed that hurdle you will need the information Miguel gave in his answer to continue.

Bart Koopman
  • 4,835
  • 17
  • 30