Click here to Skip to main content
16,015,755 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Every one,
i have one method in my project .in which i am writing xml record.
and i want to make XmlElement xDate = m_XmlDocument.CreateElement("DATE");
as date.
pl help.

my code as below

C#
       public bool InsertRecord(string date, string jobname, int hour,int tinspected, 
            int failed,double funnelpassed,double funnelfailed,
            double steampassed,double steamfailed,
            double bulbpassed,double bulbfailed,
            double constructiompasses,double constructionfailed,
            double bodypassed,double bodyfailed
            )
        {
            String filename = Application.StartupPath + REPORTFILE;
            // load  the document from a file 
            Stream xmlreadstream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            XmlTextReader reader = new XmlTextReader(xmlreadstream);

            try
            {
                m_XmlDocument.Load(reader);
                reader.Close();
                xmlreadstream.Close();

                XmlElement xDefect = m_XmlDocument.CreateElement("DEFECT");

                XmlElement xDate = m_XmlDocument.CreateElement("DATE");
                
                xDate.InnerText = date;
                XmlElement xHour = m_XmlDocument.CreateElement("HOUR");
                xHour.InnerText = Convert.ToString(hour);
                XmlElement xJobeName = m_XmlDocument.CreateElement("JOBNAME");
                xJobeName.InnerText = jobname;
                XmlElement xTInspected = m_XmlDocument.CreateElement("TOTALINSPECTED");
                xTInspected.InnerText = Convert.ToString(tinspected);
                XmlElement xFailed = m_XmlDocument.CreateElement("FAILED");
                xFailed.InnerText = Convert.ToString(failed);
                XmlElement xFunnelPassed = m_XmlDocument.CreateElement("FUNNELPASSED");
                xFunnelPassed.InnerText = Convert.ToString(funnelpassed);
                XmlElement xFunnelFailed = m_XmlDocument.CreateElement("FUANNELFAILED");
                xFunnelFailed.InnerText = Convert.ToString(funnelfailed);
                
                XmlElement xSteamPassed = m_XmlDocument.CreateElement("STEAMPASSED");
                xSteamPassed.InnerText = Convert.ToString(steampassed);
                XmlElement xSteamFailed = m_XmlDocument.CreateElement("STEAMFAILED");
                xSteamFailed.InnerText = Convert.ToString(steamfailed);

                XmlElement xBulbPassed = m_XmlDocument.CreateElement("BULBPASSED");
                xBulbPassed.InnerText = Convert.ToString(bulbpassed);
                XmlElement xBulbFailed = m_XmlDocument.CreateElement("BULBFAILED");
                xBulbFailed.InnerText = Convert.ToString(bulbfailed);

                XmlElement xConstructionPassed = m_XmlDocument.CreateElement("CONSTRUCTIONPASSED");
                xConstructionPassed.InnerText = Convert.ToString(constructiompasses);
                XmlElement xConstructionFailed = m_XmlDocument.CreateElement("CONSTRUCTIONFAILED");
                xConstructionFailed.InnerText = Convert.ToString(constructionfailed);


                XmlElement xBodyPassed = m_XmlDocument.CreateElement("BODYPASSED");
                xBodyPassed.InnerText = Convert.ToString(bodypassed);
                XmlElement xBodyFailed = m_XmlDocument.CreateElement("BODYFAILED");
                xBodyFailed.InnerText = Convert.ToString(bodyfailed);


                xDefect.AppendChild(xDate);
                xDefect.AppendChild(xHour);
                xDefect.AppendChild(xJobeName);
                xDefect.AppendChild(xTInspected);
                xDefect.AppendChild(xFailed);
                xDefect.AppendChild(xFunnelPassed);
                xDefect.AppendChild(xFunnelFailed);
                xDefect.AppendChild(xSteamPassed);
                xDefect.AppendChild(xSteamFailed);
                xDefect.AppendChild(xBulbPassed);
                xDefect.AppendChild(xBulbFailed);
                xDefect.AppendChild(xConstructionPassed);
                xDefect.AppendChild(xConstructionFailed);
                xDefect.AppendChild(xBodyPassed);
                xDefect.AppendChild(xBodyFailed);

                m_XmlDocument.DocumentElement.AppendChild(xDefect);
                

                // Save the document to a file.
                // Save the document to a file and auto-indent the output.
                Stream xmlwritestream = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
                XmlTextWriter writer = new XmlTextWriter(xmlwritestream, null);
                m_XmlDocument.Save(writer);
                writer.Close();
                xmlwritestream.Close();

            } // end try 
            catch
            {
                return false;
            }

            return true;
}
Posted

1 solution

When you write into XML, the value are finally saved as string, so you have to convert your date into a string by keeping the minimum info that you want to be saved, for example you could save only the short date.
Example:
orderElement.InnerText = order.Date.ToString("dd-MM-yyyy");
 
Share this answer
 
v2

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