| | |
How to print an XML File
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 3
dear Farid,
Read XML data into a dataset then use the next link which is used to print a dataset.
http://www.thescarms.com/dotNet/printdataset.aspx
Read XML data into a dataset then use the next link which is used to print a dataset.
http://www.thescarms.com/dotNet/printdataset.aspx
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Senior Solution Developer
Please rate my answer
A simple way to print an XM data onto the Console window is by using an object of
here is a sample code snippet:
This is one way to do it. Another way is pass an XmlTextReader object to the Load Function of the XmlDocument class.
XmlDocument.here is a sample code snippet:
C# Syntax (Toggle Plain Text)
XmlDocument doc = new XmlDocument(); doc.LoadXML(("<clientinfo><servicetype>korea</servicetype><servertype>sakray</servertype></clientinfo>")); doc.Save(Console.Out);
This is one way to do it. Another way is pass an XmlTextReader object to the Load Function of the XmlDocument class.
Thanks Amir,
Read XML data into a dataset then use the next link which is used to print a dataset.
http://www.thescarms.com/dotNet/printdataset.aspx
Thank you for your help but, This seems to be very difficult is there any easy way i.e to print the xml file directly not reading it in dataset. I want to do this in consol application any idea will be appricated.
Read XML data into a dataset then use the next link which is used to print a dataset.
http://www.thescarms.com/dotNet/printdataset.aspx
Thank you for your help but, This seems to be very difficult is there any easy way i.e to print the xml file directly not reading it in dataset. I want to do this in consol application any idea will be appricated.
Last edited by FaridMasood; Jul 22nd, 2008 at 12:48 am.
Thanks and Best of Lusk,
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
•
•
Join Date: Jul 2008
Posts: 26
Reputation:
Solved Threads: 3
dear Farid,
check the next code:
// add the next refernce to your project
System.Drawing.
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string FilePath = "File.XML";
StreamReader sr = new StreamReader(FilePath);
string text = sr.ReadToEnd();
PrintDocument doc = new ParagraphDocument(text);
doc.PrintPage += new PrintPageEventHandler(Doc_PrintPage);
doc.Print();
}
private static void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
ParagraphDocument doc = (ParagraphDocument)sender;
Font font = new Font("Arial", 15);
e.Graphics.DrawString(doc.Text, font, Brushes.Black,
e.MarginBounds, StringFormat.GenericDefault);
}
}
public class ParagraphDocument : PrintDocument
{
public string Text;
public ParagraphDocument(string text)
{
this.Text = text;
}
}
}
check the next code:
// add the next refernce to your project
System.Drawing.
using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
string FilePath = "File.XML";
StreamReader sr = new StreamReader(FilePath);
string text = sr.ReadToEnd();
PrintDocument doc = new ParagraphDocument(text);
doc.PrintPage += new PrintPageEventHandler(Doc_PrintPage);
doc.Print();
}
private static void Doc_PrintPage(object sender, PrintPageEventArgs e)
{
ParagraphDocument doc = (ParagraphDocument)sender;
Font font = new Font("Arial", 15);
e.Graphics.DrawString(doc.Text, font, Brushes.Black,
e.MarginBounds, StringFormat.GenericDefault);
}
}
public class ParagraphDocument : PrintDocument
{
public string Text;
public ParagraphDocument(string text)
{
this.Text = text;
}
}
}
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Senior Solution Developer
Please rate my answer
AmirBedair, Please use the code blocks while posting your code. Refer to the rules section of the forum.
>
This is a big no no. Do you even realize if the file is big what might happen? Never store it like that. Instead you can use the
There is also no need for you to create a child class of the PrintDocument. Inside the PrintPage Event, you can run a loop which reads from the file and then prints it. As long as there are more lines, you can continue the loop. You can do this by setting
Now, FaridMasood
>Thanks, But i want to print the xml file to printer not to consol.
>If i can print to printer by using the xmldocument object.
It doesn't matter, now that you know that you can print the XML file to the console, you can save the XML document as a file and then using the StreamReader class load the file and print it using the PrintDialog and PrintDocument Class. AmirBedair, has give you an example, but I suggest you edit the code and use proper conventions and methods. Also, while doing any of these kinds of functions should be enclosed within appropriate try...catch...finally blocks. Always call StreamReader.Close when you have finished using it.
The following conditions may cause an exception:
* You do not have permission to access the printer.
* You do not have permission to access the file system.
* There is no printer installed.
* The Arial font is not installed.
So remember to handle them properly.
>
string text = sr.ReadToEnd(); This is a big no no. Do you even realize if the file is big what might happen? Never store it like that. Instead you can use the
ReadLine() of the StreamReader class and store it. Then you can run these command in a loop. Also while specifying the path to the StreamReader class, try to give the full path.There is also no need for you to create a child class of the PrintDocument. Inside the PrintPage Event, you can run a loop which reads from the file and then prints it. As long as there are more lines, you can continue the loop. You can do this by setting
e.HasMorePages = true , as long as there is something in the sr.ReadLine() Now, FaridMasood
>Thanks, But i want to print the xml file to printer not to consol.
>If i can print to printer by using the xmldocument object.
It doesn't matter, now that you know that you can print the XML file to the console, you can save the XML document as a file and then using the StreamReader class load the file and print it using the PrintDialog and PrintDocument Class. AmirBedair, has give you an example, but I suggest you edit the code and use proper conventions and methods. Also, while doing any of these kinds of functions should be enclosed within appropriate try...catch...finally blocks. Always call StreamReader.Close when you have finished using it.
The following conditions may cause an exception:
* You do not have permission to access the printer.
* You do not have permission to access the file system.
* There is no printer installed.
* The Arial font is not installed.
So remember to handle them properly.
Last edited by ChaseVoid; Jul 22nd, 2008 at 3:00 pm.
Thank you all guys here submitted the code and instructions.
But printing in this way formatting will be omitted which is not solution of my problem i want to print the xml file with proper formating and still unable to so..
If you have some idea then do share.
But printing in this way formatting will be omitted which is not solution of my problem i want to print the xml file with proper formating and still unable to so..
If you have some idea then do share.
Thanks and Best of Lusk,
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
>But printing in this way formatting will be
>omitted which is not solution of my
>problem
What more do you want? We told you how to "get" a formatted XML output and save it. We also told you how to print a file. All you need to do is combine those two. Do you think we are here just do your homework while you just sit there and have the answers for nothing. Please atleast "try" to use some of your basic c# knowledge, otherwise no one will ever help you.
>omitted which is not solution of my
>problem
What more do you want? We told you how to "get" a formatted XML output and save it. We also told you how to print a file. All you need to do is combine those two. Do you think we are here just do your homework while you just sit there and have the answers for nothing. Please atleast "try" to use some of your basic c# knowledge, otherwise no one will ever help you.
•
•
•
•
>But printing in this way formatting will be
>omitted which is not solution of my
>problem
What more do you want? We told you how to "get" a formatted XML output and save it. We also told you how to print a file. All you need to do is combine those two. Do you think we are here just do your homework while you just sit there and have the answers for nothing. Please atleast "try" to use some of your basic c# knowledge, otherwise no one will ever help you.
Thanks and Best of Lusk,
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
![]() |
Similar Threads
- XML Bug Error. (XML, XSLT and XPATH)
- write/Read into XML File Using VC++ 6.0 please help me (XML, XSLT and XPATH)
- read xml using vb.net (VB.NET)
- Questions about file uploads (PHP)
- create pdf or word file from within c# (C#)
- How to read the parsed XML file (DOM parser) (Java)
- 500 ways to print [1..10]! (IT Professionals' Lounge)
- xml and p (Python)
- xml file parsing in c++ (C++)
- parsing error (XML file-java-schema) (Java)
Other Threads in the C# Forum
- Previous Thread: Array v/s Double
- Next Thread: reading time stamp
| Thread Tools | Search this Thread |
.net access algorithm array barchart bitmap box broadcast c# check checkbox client combobox control conversion csharp custom cyclethruopenforms data database datagrid datagridview dataset date/time datetime degrees development draganddrop drawing encryption enum event excel file filename finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile gis globalization gtk httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remoting richtextbox save server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml





