Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello all,
I am trying to create and xml with the help of c# object.
right now i am having one order object and with the help of XslCompiledTransform i want to create a xml file by applying xsl on it.
But XslCompiledTransform take the parameter as string input uri or xml reader

so my question is can i create the xmlreader object by passing my orderObject so that i can further use the xmlreader for creating the xml file by passing it to XslCompiledTransform.
My Order object
C#
public class OrderObject
{
    public string OrderName { get; set; }
    public string Price { get; set; }
}

and on button click i am doing some thing like
C#
XmlWriterSettings writerSettings = new XmlWriterSettings(); //Object of XmlWriterSettings to generate the xml output.
        writerSettings.Encoding = Encoding.GetEncoding("ISO-8859-1");
        XsltArgumentList xsltArgumentList = new XsltArgumentList();
        XmlWriter writer = null;

        try
        {
            writer = XmlWriter.Create(_outputPath, writerSettings); //Start Creating the xml document
            #if DEBUG
            XslCompiledTransform transform = new XslCompiledTransform(true);
            #else
            XslCompiledTransform transform = new XslCompiledTransform();
            #endif

            XsltSettings xsltSettings = new XsltSettings(); //Create object of XsltSettings and set its EnableScript = true
            xsltSettings.EnableScript = true;

            transform.Load(_xslFilePath, xsltSettings, new XmlUrlResolver()); //Load master.xml with xsltSettings

            List<OrderObject> newOrderData = OrderData.NewOrder;

            XmlReader xmlReader = XmlReader.Create(); //????
            transform.Transform(xmlReader, xsltArgumentList, writer); //Apply xsl and output xml 

            writer.Flush(); //flush the writer buffer

            writer.Close(); //close the writer
            lblMessage.Text = "XML Output file is generated successfully Path:" + _outputPath;
        } //try
        catch (Exception ex)
        {
            writer.Close(); //if error occur then close the writer
            lblMessage.Text = "Error occurred while generating the xml!" + ex.Message;
        } //catch


At the below line in code i m getting stuck-up
C#
 List<OrderObject> newOrderData = OrderData.NewOrder;
XmlReader xmlReader = XmlReader.Create(); //????</pre>


Is there any way that i can use the orderobjectlist for creating the xml
or Is there any other way i can work through the order object

Thanks in advance
Posted

1 solution

Solved:
the below method ObjectToXml take the obj and filename as the parameters and with the help of XmlSerializer it create the xml file on the given path

C#
namespace Utility
{
    public class Serial
    {
        public static void ObjectToXml(object obj, string path_to_xml)
        {
            //serialize and persist it to it's file
            try
            {
                if(File.Exists(path_to_xml))
                    File.Delete(path_to_xml);
                XmlSerializer ser = new XmlSerializer(obj.GetType(),new XmlRootAttribute("document"));
                FileStream fs = File.Open(
                        path_to_xml,            // File Path
                        FileMode.OpenOrCreate,  // File Mode
                        FileAccess.Write,       // File Access
                        FileShare.ReadWrite);   // File Share 
                ser.Serialize(fs, obj);
                fs.Close();     // Close the file
            }
            catch (Exception ex)
            {
                throw new Exception("Could Not Serialize the object to " + path_to_xml, ex);
            }
 
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900