Export content from InDesign and deliver it to your digital service
8 min read27 March 2018
We all use a variety of systems on a daily basis. You might have content in a catalogue system, a learning management system, or publishing software. When you want to use this content elsewhere – say on your website, in an app, or in printed material – you probably have to resort to copying and pasting content from one system to another.
Download the source code from GitHub
12345678910111213
<!ELEMENT Cover (CoverTitle,CoverSubtitle)>
<!ELEMENT CoverTitle (#PCDATA)>
<!ELEMENT CoverSubtitle (#PCDATA)>
<!ELEMENT Stories (Story*)>
<!ELEMENT Story (StoryHeading*,StoryIntroduction*,StorySubHeading*,StoryText*,StoryImage*,StorySubHeading*,StoryImage*,FeatureImage*)*>
<!ELEMENT StoryHeading (#PCDATA)>
<!ELEMENT StoryIntroduction (#PCDATA)>
<!ELEMENT StoryText (#PCDATA)>
<!ELEMENT StorySubHeading (#PCDATA)>
<!ELEMENT StoryImage EMPTY>
<!ATTLIST StoryImage href CDATA "">
<!ELEMENT FeatureImage EMPTY>
<!ATTLIST FeatureImage href CDATA "">
1234
// Create the management client
var client = ManagementClient.Create("[ADD CMS URL HERE]",
"[ADD YOUR API CLIENT ID HERE]",
"[ADD YOUR API SHARED SECRET HERE]");
12
// Get the project
var project = client.Projects.Get("[ADD YOUR CONTENSIS PROJECT NAME HERE]");
12
// Load the XML file into an XElement
XElement doc = XElement.Load(@"C:\Sites\indesign-import-to-contensis\dotnet-xml-import\XMLImport\finalXML.xml");
123456789101112131415161718192021222324
// Load the XML file into an XElement
XElement doc = XElement.Load(@"C:\Sites\indesign-import-to-contensis\dotnet-xml-import\XMLImport\finalXML.xml");
// Navigate the XElement to create the stories
IEnumerable<Story> stories = (from x in doc.Descendants("Stories").Elements("Story")
select new Story
{
Title = x.Element("StoryHeading").Value,
Introduction = x.Element("StoryIntroduction").Value,
StoryBody = x.Element("StoryBody").Elements()
}).ToList();
// Local testing in console
//Console.WriteLine("Story title: " + stories.First().Title);
//Console.WriteLine("Story Intro: " + stories.First().Introduction);
//Console.WriteLine("Story Body: " + stories.First().StoryBody);
//Console.ReadLine();
// Iterate through stories and call entry creator
foreach (var story in stories)
{
CreateEntryFromXML(project, story);
}
1234567891011121314151617181920212223242526272829
// Create entries
private static void CreateEntryFromXML(Project project, Story story)
{
// Set-up a new story entry
var storyEntry = project.Entries.New("story");
var composer = new ComposedField();
string html = "";
foreach (XElement el in story.StoryBody)
{
html = html + ElementCleaner(el);
}
byte[] bytes = Encoding.Default.GetBytes(html);
html = Encoding.UTF8.GetString(bytes);
composer.Add(new ComposedFieldItem("storyMarkup", html));
storyEntry.Set("storyComposer", composer);
// Set each field value
storyEntry.Set("title", story.Title);
storyEntry.Set("subtitle", story.Introduction);
// Save and publish the story
storyEntry.Save();
storyEntry.Publish();
Console.WriteLine({{APP}}quot;Saved story {storyEntry.Get("title")}");
}
1234567891011121314
private static string ElementCleaner(XElement el)
{
string s = "";
switch (el.Name.LocalName)
{
case "StoryText":
s = "<p>" + el.Value + "</p>";
break;
case "StorySubHeading":
s = "<h2>" + el.Value + "</h2>";
break;
}
return s;
}