如何将xmlnode类型设置为日期。 [英] how to set xmlnode type as date .
本文介绍了如何将xmlnode类型设置为日期。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
i在我的项目中有一个方法。我正在编写xml记录。
我想制作XmlElement xDate = m_XmlDocument.CreateElement( DATE);
作为日期。
pl帮助。
我的代码如下
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
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;
}
推荐答案
当您写入XML时,该值最终保存为字符串,因此您必须转换日期通过保留您想要保存的最小信息进入字符串,例如,您只能保存短日期。
示例:
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");
这篇关于如何将xmlnode类型设置为日期。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文