XML to Word Document

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boyindie86
    New Member
    • Jun 2007
    • 31

    XML to Word Document

    Hi Forum,

    I have been trying to create an automated report in word, using VB.NET.

    I am trying to build a program, which will read an XML template of the word document, which should traverse through the XML document, and create the desired elements from the name of the XML tags.

    My xml file looks like the following
    Code:
    <Document>
    <Heading1>
    <Title> 1.0 Introduction</Title>
    <Heading2>
    <Title> 1.1 Test Laboratory</Title>
    <FeatTable>
    <row id="1">
    <Row1>Telephone</Row1>
    <FeatId>7</FeatId>
    <FeatItem>1</FeatItem>
    </row>
    <row id="1">
    <FeatTiltle>Fax</FeatTiltle>
    <FeatId>7</FeatId>
    <FeatItem>2</FeatItem>
    </row>
    <row id="2">
    <FeatTiltle>Email</FeatTiltle>
    <FeatId>7</FeatId>
    <FeatItem>3</FeatItem>
    </row>
    <row id="3">
    <FeatTiltle>Website</FeatTiltle>
    <FeatId>7</FeatId>
    <FeatItem>4</FeatItem>
    </row>
    </FeatTable>
    </Heading2>
    <Heading2>
    <Title> 1.2 Test Location</Title>
    <FeatTable>
    <row id="1">
    <Row1>Telephone</Row1>
    <FeatId>7</FeatId>
    <FeatItem>1</FeatItem>
    </row>
    <row id="2">
    <FeatTiltle>Website</FeatTiltle>
    <FeatId>7</FeatId>
    <FeatItem>4</FeatItem>
    </row>
    </FeatTable>
    </Heading2>
    </Heading1>
    </Document>
    So say if my code hits the heading1 tag, it will create an heading 1 in word, and it should then look for the title attribute which goes with the heading and fill in the text into the word document.

    I am having problems reading the XML document into VB.NET. I don't know the cleanest way to traverse the document from top to bottom, but also get the code to return a list of all the child nodes from specific tags.

    Eg if i hit the FeatTable tag, I want it to create a table in the word document and then return all the tables rows, so that they can be printed into the document.

    I want my code to be somthing like:
    Code:
    Public Sub main(ByVal xmlFile As String)
            
            Dim reader As XmlTextReader = New XmlTextReader(xmlFile)
            Dim val As String
    
     
            Do While reader.Read
    
                Select Case reader.NodeType
                    Case XmlNodeType.Element
                        Select Case reader.Name
                            Case "Document"
                            'CREATE NEW WORD DOCUMENT
    
                            Case "Heading1"
                            'Create top most heading
                            'Write heading title from title tag
                            Case "Heading2"
                            'Create sub heading
                            'Write heading title from title tag
                         Case "FeatTable"
                          'CREATE TABLE
                          'Get Table rows from file
    
                        End Select
                End Select
    
            Loop
            WordDoc.PrintOut()
        End Sub
    I am happy with the word side of things its just the traversing of the XML document thats causing me great headaches.

    Any help would be greatly appreciated!

    Boyindie
    Last edited by Frinavale; Jan 25 '10, 04:24 PM. Reason: Please post code in [code] ... [/code] tags. Changed Quote tags into Code tags.
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Have you considered using the XMLReader class to help you achieve what you want to achieve?

    -Frinny

    Comment

    • mzmishra
      Recognized Expert Contributor
      • Aug 2007
      • 390

      #3
      Look into this article

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        I don't like stumbling through the DOM with the System.Xml namespace. I prefer to use LINQ to XML (the System.Xml.Linq and System.Linq namespaces).

        Use XDocument.Load to create an XDocument, then use Linq to traverse it properly.

        I've been meaning to do a bit of a LINQ writeup, but I haven't had a chance.

        Here's a program I wrote that will give you access to the data on each tier:
        Code:
        using System;
        using System.Collections.Generic;
        using System.IO;
        using System.Linq;
        using System.Xml.Linq;
        
        namespace HelpForBytes
        {
            class Program
            {
                static void Main(string[] args)
                {
                    TextReader reader = new StreamReader(@"C:\dev\temp.xml");
                    XDocument doc = new XDocument();
                    doc = XDocument.Load(reader);
        
                    List<XElement> h1s = doc.Descendants().Where(x => x.Name == "Heading1").ToList();
                    foreach (XElement h1 in h1s)
                    {
                        string h1Title = h1.Element("Title").Value;
                        Console.WriteLine(h1Title);
                        List<XElement> h2s = h1.Descendants("Heading2").ToList();
                        foreach (XElement h2 in h2s)
                        {
                            string h2Title = h2.Element("Title").Value;
                            Console.WriteLine(h2Title);
                            List<Row> rows = (from x in h2.Element("FeatTable").Descendants()
                                              where x.Name == "row"
                                              select new Row
                                              {
                                                  FeatTitle = (x.Element("Row1") == null) ? x.Element("FeatTiltle").Value : x.Element("Row1").Value,
                                                  FeatId = int.Parse(x.Element("FeatId").Value),
                                                  FeatItem = int.Parse(x.Element("FeatItem").Value)
                                              }).ToList();
                            foreach (Row row in rows)
                            {
                                Console.WriteLine(row.FeatTitle);
                                Console.WriteLine(row.FeatId);
                                Console.WriteLine(row.FeatItem);
                            }
                        }
                    }
                    Console.ReadKey();
                }
            }
        
            public class Row
            {
                public string FeatTitle { get; set; }
                public int FeatId { get; set; }
                public int FeatItem { get; set; }
            }
        }

        Comment

        Working...