Use Linq to XML with Flare Files (part 1)

Linq to XML is a library for XML manipulation for use with Microsoft’s Visual Basic and Visual C#. Linq to XML offers extensive functionality at the cost of limiting development to a .NET context. Microsoft posts information about Linq to XML here:

http://msdn.microsoft.com/en-us/library/bb387098

The Visual Basic manifestation of the System.XML.Linq interface is particularly interesting because it allows you to work with XML markup in-line with the Visual Basic code as an XML literal. For example, to create a new Flare topic from an application which uses Linq to XML, the code could look like this:

Imports System.Xml.Linq
Module Module1
    Sub Main()
        Dim FlareTopicDoc As XDocument = _
            <?xml version="1.0" encoding="utf-8"?>
            <html xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">
                <head>
                </head>
                <body>
                    <h1>New Flare Topic</h1>
                    <p>This was created with a command line application.</p>
                </body>
            </html>

        FlareTopicDoc.Save("C:\FlareExamples\FlareTopicDoc.htm")
    End Sub
End Module

That document could also have been created with Linq to XML with C#:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXMLExampleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            XNamespace n = "http://www.madcapsoftware.com/Schemas/MadCap.xsd";
            XDocument FlareTopicDoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XElement(n + "html",
                    new XElement(n + "head"),
                    new XElement(n + "body",
                        new XElement(n + "h1", "New Flare Topic"),
                        new XElement(n + "p", "This was created with a command line application.")
                        )
                    )
                );

            FlareTopicDoc.Save("C:\\FlareExamples\\FlareTopicDoc.htm");
        }
    }
}

Leave a comment

Your email address will not be published.

HTML tags are not allowed.

239,960 Spambots Blocked by Simple Comments