How to print an XML File

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2007
Posts: 59
Reputation: FaridMasood is an unknown quantity at this point 
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

How to print an XML File

 
0
  #1
Jul 21st, 2008
If someone has any idea that how to print the xml file.
Thanks and Best of Lusk,

Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: AmirBedair is an unknown quantity at this point 
Solved Threads: 3
AmirBedair AmirBedair is offline Offline
Light Poster

Re: How to print an XML File

 
0
  #2
Jul 21st, 2008
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
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: How to print an XML File

 
0
  #3
Jul 21st, 2008
A simple way to print an XM data onto the Console window is by using an object of XmlDocument.

here is a sample code snippet:
  1. XmlDocument doc = new XmlDocument();
  2. doc.LoadXML(("<clientinfo><servicetype>korea</servicetype><servertype>sakray</servertype></clientinfo>"));
  3.  
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: FaridMasood is an unknown quantity at this point 
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

Re: How to print an XML File

 
0
  #4
Jul 22nd, 2008
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.
Last edited by FaridMasood; Jul 22nd, 2008 at 12:47 am.
Thanks and Best of Lusk,

Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: FaridMasood is an unknown quantity at this point 
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

Re: How to print an XML File

 
0
  #5
Jul 22nd, 2008
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.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 26
Reputation: AmirBedair is an unknown quantity at this point 
Solved Threads: 3
AmirBedair AmirBedair is offline Offline
Light Poster

Re: How to print an XML File

 
0
  #6
Jul 22nd, 2008
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;
}
}
}
Amir S.Bedair
Senior Solution Developer
Please rate my answer
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: How to print an XML File

 
0
  #7
Jul 22nd, 2008
AmirBedair, Please use the code blocks while posting your code. Refer to the rules section of the forum.

> 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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: FaridMasood is an unknown quantity at this point 
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

Re: How to print an XML File

 
0
  #8
Jul 24th, 2008
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.
Thanks and Best of Lusk,

Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 111
Reputation: ChaseVoid is an unknown quantity at this point 
Solved Threads: 12
ChaseVoid's Avatar
ChaseVoid ChaseVoid is offline Offline
Junior Poster

Re: How to print an XML File

 
0
  #9
Jul 24th, 2008
>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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 59
Reputation: FaridMasood is an unknown quantity at this point 
Solved Threads: 0
FaridMasood's Avatar
FaridMasood FaridMasood is offline Offline
Junior Poster in Training

Re: How to print an XML File

 
0
  #10
Jul 25th, 2008
Originally Posted by ChaseVoid View Post
>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.
If you cant understand any problem then dont post your nonsense answers to get increased posts. Understand.
Thanks and Best of Lusk,

Farid ud din Masood
MS.c (CS)
University of Agriculture, Faisalabad
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC