If someone has any idea that how to print the xml file.

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:

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, But i want to print the xml file to printer not to consol. If i can print to printer by using the xmldocument object.

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.

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;
        }
    }
}

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.

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
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.

If you cant understand any problem then dont post your nonsense answers to get increased posts. Understand.

Dear ChaseVoid ,

Thanx for your notes about using code blocks..

what i did is a sample of quieck code to solve a the problem, anyway I know maybe something missing I want to help nothing more...

but I noticed you replied here not to solve the problem just to check others replies.. if you have a solution you should write it , and don't comment on other replies ...

learn how to prove others faults with your answer...


Dear Farid,

please check th all code and you will see the answer , please try to depend on yourself to write the code, I wished to be your assist as clearing the way, and you should continue customizing the code to solve your problem

Save it as a different file type (for example, save as PowerPoint presentation). Then it will print just fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.