Answered by:
Generating A Word 2010 Document based on XML File

Question
-
User2116114096 posted
Hello frnds
I am having an large XML file generated on servers containing the information like SAN, HOSTNAME,IP's, WWN . . etc.My plan is to convert this XML report in to word format i.e, on Word 2010 .docx file. For this I gon through the link
http://msdn.microsoft.com/en-us/library/ee872374(office.12).aspx and along with the video shown in this link explained in simple way and clear cut information for each stages like XML->XSLT->HTML->WORD DOCUMENT
And here they used word 2007 ver to generate the report
The problem which am facing when going through the same steps .. Is am unable to come with the solution based on my XML file, am getting that word file but without any output when am running the solution file based on source file i.e, XML and XSLT.
And its a very large XML file it includes nearly 10 servers details in one XML file.
The example they took in the above link is very simple but mine is its real server report ..
I gon through in detail to build my document as they done, but am still unable to generate the report ..
If u go through that video, you will come to know its just a 9min video
I'll paste some XML code here .. [as I don't know how to attach the file with this post]
Please let me know if any one have any idea how to workout with this kind of large XML file ..
Friends, I really need your suggestions ..<SAN:SAN type="Category">
<SAN:Servers type="Container">
<SAN:Server type="Category">
<SAN:HostName type="Property">INKADCPCONT.lvgi.com</SAN:HostName>
<SAN:HostIPAddress type="Property">10.134.11.122</SAN:HostIPAddress>
<SAN:HostID type="Property">c867d384-48f3-4d5e-8d8e-1dc02febfe06</SAN:HostID>
<SAN:WWN type="Property">c867d384-48f3-4d5e-8d8e-1dc02febfe06</SAN:WWN>
<SAN:OSName type="Property">Windows Server 2008 R2 family</SAN:OSName>
<SAN:IsManuallyRegistered type="Property">false</SAN:IsManuallyRegistered>
<SAN:IsExpandable type="Property">false</SAN:IsExpandable>
<SAN:PollType type="Property">4</SAN:PollType>
<SAN:IfServerSupportSmartPoll type="Property">true</SAN:IfServerSupportSmartPoll>
<SAN:NumberOfLUNs type="Property">1</SAN:NumberOfLUNs>
<SAN:IsManaged type="Property">true</SAN:IsManaged>
<SAN:OSVersionAsString type="Property">6.1 (Build 7601) Service Pack 1.</SAN:OSVersionAsString>
<SAN:PlatformName type="Property">Windows NT</SAN:PlatformName>
<SAN:HBAInfo type="Category">
<SAN:NumberOfHBAPorts type="Property">2</SAN:NumberOfHBAPorts>
<SAN:HostLoginStatus type="Property">0</SAN:HostLoginStatus>
<SAN:HostManagementStatus type="Property">2</SAN:HostManagementStatus>
<SAN:IsAttachedHost type="Property">true</SAN:IsAttachedHost>
<SAN:LoggedInPorts type="PropertyArray">
<SAN:LoggedInPort type="Property">3</SAN:LoggedInPort>
<SAN:LoggedInPort type="Property">0</SAN:LoggedInPort>
<SAN:LoggedInPort type="Property">2</SAN:LoggedInPort>
<SAN:LoggedInPort type="Property">1</SAN:LoggedInPort>
</SAN:LoggedInPorts>
Thank You
praveen
Monday, August 5, 2013 11:21 AM
Answers
-
User697462465 posted
Hi praveengb
Based on your description, I have created a sample, please try to refer to the following code:
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); XDocument xDoc = XDocument.Load(%xmlFileName%); foreach (XElement xeServers in xDoc.Root.Elements("Servers")) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 1; doc.Paragraphs.Last.Range.Text = xeServers.Name.ToString(); foreach (XElement xeServer in xeServers.Elements("Server")) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 1; doc.Paragraphs.Last.Range.Text = " " + xeServer.Name.ToString(); foreach (XElement xeNode in xeServer.Elements()) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 0; doc.Paragraphs.Last.Range.Text = " " + xeNode.Name.ToString() + ": " + xeNode.Value; } } } doc.SaveAs2(%docFileName%); doc.Close(); appWord.Quit();
BTW, There is a .Net library named ”Spire.Doc” which can help you to convert xml to word easily, but it is not free to use. For more information please try to refer to
http://www.e-iceblue.com/Introduce/word-for-net-introduce.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 12, 2013 6:17 AM -
User697462465 posted
Hi praveengb,
According to your error, I think you might not Refer correct namespace, refer to the correct namespace below:
1. XElement,XDocument is belong to using System.Xml.Linq namespace;
2.Microsoft.Office.Interop.Word, it is premised on the need to install Microsoft Office, and you have to add the reference manually in Project.
Hope it can help you.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 16, 2013 7:51 AM
All replies
-
User-359936451 posted
You might want to try an OFFICE forum...
Monday, August 5, 2013 3:30 PM -
User2116114096 posted
But the main task in this thread is to converting the XML to XSLT form ..
Tuesday, August 6, 2013 7:04 AM -
User697462465 posted
Hi praveengb
Based on your description, I have created a sample, please try to refer to the following code:
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); XDocument xDoc = XDocument.Load(%xmlFileName%); foreach (XElement xeServers in xDoc.Root.Elements("Servers")) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 1; doc.Paragraphs.Last.Range.Text = xeServers.Name.ToString(); foreach (XElement xeServer in xeServers.Elements("Server")) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 1; doc.Paragraphs.Last.Range.Text = " " + xeServer.Name.ToString(); foreach (XElement xeNode in xeServer.Elements()) { doc.Paragraphs.Add(); doc.Paragraphs.Last.Range.Bold = 0; doc.Paragraphs.Last.Range.Text = " " + xeNode.Name.ToString() + ": " + xeNode.Value; } } } doc.SaveAs2(%docFileName%); doc.Close(); appWord.Quit();
BTW, There is a .Net library named ”Spire.Doc” which can help you to convert xml to word easily, but it is not free to use. For more information please try to refer to
http://www.e-iceblue.com/Introduce/word-for-net-introduce.html
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, August 12, 2013 6:17 AM -
User2116114096 posted
hello Terry
Thanks for ur time and response .. to execute ur code I used the below assembly's, but when I build the this solution file I came up with an error's like ...
The namespace's like Office,XElement,XDocument doesnot exist "are u missing any assembly reference?"
See is there any other assemblies to include in this code ..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;namespace XSLTWordDoc
{
class Program
{
static void Main(string[] args)
{
Microsoft.Office.Interop.Word.Application appWord = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
XDocument xDoc = XDocument.Load(@"C:\temp\mysans.xml");foreach (XElement xeServers in xDoc.Root.Elements("Servers"))
{
doc.Paragraphs.Add();
doc.Paragraphs.Last.Range.Bold = 1;
doc.Paragraphs.Last.Range.Text = xeServers.Name.ToString();foreach (XElement xeServer in xeServers.Elements("Server"))
{
doc.Paragraphs.Add();
doc.Paragraphs.Last.Range.Bold = 1;
doc.Paragraphs.Last.Range.Text = " " + xeServer.Name.ToString();foreach (XElement xeNode in xeServer.Elements())
{
doc.Paragraphs.Add();
doc.Paragraphs.Last.Range.Bold = 0;
doc.Paragraphs.Last.Range.Text = " " + xeNode.Name.ToString() + ": " + xeNode.Value;
}
}
}
doc.SaveAs2(@"C:\temp\MySanDoc.docx");
doc.Close();
appWord.Quit();
}
}
}Friday, August 16, 2013 3:12 AM -
User697462465 posted
Hi praveengb,
According to your error, I think you might not Refer correct namespace, refer to the correct namespace below:
1. XElement,XDocument is belong to using System.Xml.Linq namespace;
2.Microsoft.Office.Interop.Word, it is premised on the need to install Microsoft Office, and you have to add the reference manually in Project.
Hope it can help you.
Best Regards,
Terry Guo- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, August 16, 2013 7:51 AM -
User2116114096 posted
hello Terry
Yes I included DocumnetFormat.OpenXML and WindowsBase references in my project, but seems these are not the appropriate references .. It's coming with an 'Office' doesnot exist in Microsoft namespace ..
And other thing, friend am not professional developer on this field , It's suddenly I got a work with generating a report on XML file ..and I have some basic knowledge and worked on ASP.Net with 3.5 framework earlier ..
So can u help me on this .. adding proper references and building up with the solution for this task ..
Thanks
praveen
Friday, August 23, 2013 12:04 AM -
User697462465 posted
Hi,
Please follow below steps to setup your project:
- Install Microsoft Office if you don't have installed it.
- Right click your project in Visual Studio, then select Add reference.
- Select the .Net tab, then select Microsoft.Office.Interop.Word item.
- Rebuild your project.
- Now it should be able to work fine.
If you have any question, please let me know.
Best Regards,
Terry GuoFriday, August 23, 2013 11:59 AM -
User2116114096 posted
Terry
So far I was working on the Project as a ConsoleApplication, Is this be fine .. or should I go for building the project by using ASP.Net Web Application Template or Windows Forms Application Template
Cos when am debugging the solution file the new error popping up as "C:\temp\XSLTWordDoc\\DEBUG\BIN\XSLTWordDoc.exe" is missing, and cannot start debugging,
but in this path the filename XSLTWordDoc.vshost.exe and XSLTWordDoc.vshost.exe.manifest are locating
Thanks praveen
Monday, September 2, 2013 1:46 AM