Discussion:
請問在WebApplication專案內如何存XML檔案
(时间太久无法回复)
unknown
2006-06-16 02:04:02 UTC
Permalink
我寫了一個class,class內有個function可把傳入的字串陣列寫入xml檔案,這個class在Windows
Application自己測試是是沒問題,但是把class放到Web Application專案內使用,就會有問題,以下是程式碼:

public class SchedulerXmlUtil
{
static string initTmpXmlFileName =
"SchedulerXmlUtil_init_tmp.xml";//作為原生xml->xsd的中繼檔
string xmlFileName;
string xsdFileName;

public SchedulerXmlUtil(string filename)//建構子,寫入基本內容
{
xmlFileName = filename.Clone().ToString();
xsdFileName = "scheOri.xsd";

XmlTextWriter myXmlTextWriter = new XmlTextWriter (xmlFileName, null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(true);

myXmlTextWriter.WriteStartElement("scheduleWorkPool");
myXmlTextWriter.WriteStartElement("scheduleWork");
myXmlTextWriter.WriteElementString("rowNum", null, "rowNum - Do not
remove this element.");//rowNum
myXmlTextWriter.WriteElementString("workNameSpace", null, "workNameSpace
- Do not remove this element.");//workNameSpace
myXmlTextWriter.WriteElementString("workName", null, "workName - Do not
remove this element.");//workName
myXmlTextWriter.WriteElementString("workParameter", null, "workParameter
- Do not remove this element.");//workParameter
myXmlTextWriter.WriteElementString("workStatus", null, "workStatus - Do
not remove this element.");//workStatus
myXmlTextWriter.WriteElementString("workStartTime", null, "workStartTime
- Do not remove this element.");//workStartTime
myXmlTextWriter.WriteElementString("workEndTime", null, "workEndTime - Do
not remove this element.");//workEndTime
myXmlTextWriter.WriteElementString("loopMonth", null, "loopMonth - Do not
remove this element.");//loopMonth
myXmlTextWriter.WriteElementString("loopDay", null, "loopDay - Do not
remove this element.");//loopDay
myXmlTextWriter.WriteElementString("loopWeek", null, "loopWeek - Do not
remove this element.");//loopWeek
myXmlTextWriter.WriteElementString("loopHour", null, "loopHour - Do not
remove this element.");//loopHour
myXmlTextWriter.WriteElementString("loopMinute", null, "loopMinute - Do
not remove this element.");//loopMinute
myXmlTextWriter.WriteElementString("rid", null, "rid - Do not remove this
element.");//rid
myXmlTextWriter.WriteElementString("lastExecutionTime", null,
"lastExecutionTime - Do not remove this element.");//lastExecutionTime
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();

myXmlTextWriter.Flush();
myXmlTextWriter.Close();
}


public bool AddRow(string[] rowData)
{
XmlDataDocument datadoc = new XmlDataDocument();
StreamReader XmlStreamReader = null;
StreamWriter XmlStreamWriter = null;
try
{
XmlStreamReader = new StreamReader(xmlFileName);
datadoc.DataSet.ReadXml(XmlStreamReader, XmlReadMode.InferSchema);
}
catch (Exception ex)
{
return false;
}
finally
{
XmlStreamReader.Close();
}
DataSet ds1 = datadoc.DataSet;

DataRow dr1 = ds1.Tables["scheduleWork"].NewRow();
dr1["rowNum"] = rowData[0].Clone().ToString();/////<--出錯在這一行
dr1["workNameSpace"] = rowData[1].Clone().ToString();
dr1["workName"] = rowData[2].Clone().ToString();
dr1["workParameter"] = rowData[3].Clone().ToString();
dr1["workStatus"] = rowData[4].Clone().ToString();
dr1["workStartTime"] = rowData[5].Clone().ToString();
dr1["workEndTime"] = rowData[6].Clone().ToString();
dr1["loopMonth"] = rowData[7].Clone().ToString();
dr1["loopDay"] = rowData[8].Clone().ToString();
dr1["loopWeek"] = rowData[9].Clone().ToString();
dr1["loopHour"] = rowData[10].Clone().ToString();
dr1["loopMinute"] = rowData[11].Clone().ToString();
dr1["rid"] = rowData[12].Clone().ToString();
dr1["lastExecutionTime"] = rowData[13].Clone().ToString();

ds1.Tables["scheduleWork"].Rows.Add(dr1);

try
{
XmlStreamWriter = new StreamWriter(xmlFileName);
datadoc.DataSet.WriteXml(XmlStreamWriter, XmlWriteMode.WriteSchema);
}
catch (Exception ex)
{
return false;

}
finally
{
XmlStreamWriter.Close();
}
return true;
}//AddRow

}//class


在 Web Application的呼叫程式碼為

SchedulerXmlUtil sxu = new SchedulerXmlUtil(scheduleFile);
string[] xmlDataArray = new string[14];
dr[0] = "<a href='javascript:loadRow("+(rowCount+1)+");'>載入</a>";
dr[1] = "<input type='submit' name='DelButton' value='刪除'
id='DelButton"+(rowCount+1)+"' onclick='deleteRow("+(rowCount+1)+");'/>";
dr[2] = workNameSpace.Text;//命名空間
dr[3] = workName.SelectedValue;//工作名稱
dr[4] = workParameter.Text;//參數
dr[5] = "停用";//狀態
dr[6] = workStartTime.ToString();//開始時間
dr[7] = workEndTime.ToString();//失效時間
dr[8] = getLoopTime(loopMonth2, selectAllMonth);//循環
dr[9] = getLoopTime(loopDay2, selectAllDay);//循環
dr[10] = getLoopTime(loopWeek2, selectAllWeek);//循環
dr[11] = getLoopTime(loopHour2, selectAllHour);//循環
dr[12] = getLoopTime(loopMinute2, selectAllMinute);//循環
dr[13] = "<input type='submit' name='changeStatusButton' value='啟用'
id='changeStatusButton"+(rowCount+1)+"'
onclick='changeRowStatus("+(rowCount+1)+");'/>";;//status
sxu.AddRow(xmlDataArray);


只要執行到所叫用的程式碼,就會出現以下錯誤:

並未將物件參考設定為物件的執行個體
描述: 在執行目前 Web 要求的過程中發生未處理的例外情形。請檢閱堆疊追蹤以取得錯誤的詳細資訊,以及在程式碼中產生的位置。
例外詳細資訊: System.NullReferenceException: 並未將物件參考設定為物件的執行個體
然後錯誤行指到AddRow()內的
dr1["rowNum"] = rowData[0].Clone().ToString();/////<--出錯在這一行

堆疊追蹤:
[NullReferenceException: 並未將物件參考設定為物件的執行個體]
WA_setSchedule_onC.SchedulerXmlUtil.AddRow(String[] rowData) in
c:\inetpub\wwwroot\wa_setschedule_onc\schedulerxmlutil.cs:174
WA_setSchedule_onC.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\wa_setschedule_onc\webform1.aspx.cs:431
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()



請問,有可能是那邊出了問題呢?

謝謝
Bird
2006-06-17 08:50:46 UTC
Permalink
±q¿ù»~¬Ý°T®§šÓ¬Ý¡AÀ³žÓ¬O³oŠæ

dr1["rowNum"] = rowData[0].Clone().ToString();

žÌ­±ªº rowData[0] šSŠ³ªFŠè (null) ©ÎªÌ¬O dr1["rowNum"] šSŠ³ªFŠè¡C
«Øij³æšB°£¿ù¬Ý¬Ý­þžÌ¥X€F°ÝÃD¡C


Tsai Bird
MCTS for Web, MCSD.NET, MCAD
ApplicationŠÛ€vŽúžÕ¬O¬OšS°ÝÃD¡AŠý¬O§âclass©ñšìWeb Application±M®×€ºšÏ¥Î¡AŽN·|Š³°ÝÃD¡A¥H€U¬Oµ{Š¡œX¡G
public class SchedulerXmlUtil
{
static string initTmpXmlFileName =
string xmlFileName;
string xsdFileName;
public SchedulerXmlUtil(string filename)//«Øºc€l¡AŒg€J°ò¥»€º®e
{
xmlFileName = filename.Clone().ToString();
xsdFileName = "scheOri.xsd";
XmlTextWriter myXmlTextWriter = new XmlTextWriter (xmlFileName, null);
myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument(true);
myXmlTextWriter.WriteStartElement("scheduleWorkPool");
myXmlTextWriter.WriteStartElement("scheduleWork");
myXmlTextWriter.WriteElementString("rowNum", null, "rowNum - Do not
remove this element.");//rowNum
myXmlTextWriter.WriteElementString("workNameSpace", null, "workNameSpace
- Do not remove this element.");//workNameSpace
myXmlTextWriter.WriteElementString("workName", null, "workName - Do not
remove this element.");//workName
myXmlTextWriter.WriteElementString("workParameter", null, "workParameter
- Do not remove this element.");//workParameter
myXmlTextWriter.WriteElementString("workStatus", null, "workStatus - Do
not remove this element.");//workStatus
myXmlTextWriter.WriteElementString("workStartTime", null, "workStartTime
- Do not remove this element.");//workStartTime
myXmlTextWriter.WriteElementString("workEndTime", null, "workEndTime - Do
not remove this element.");//workEndTime
myXmlTextWriter.WriteElementString("loopMonth", null, "loopMonth - Do not
remove this element.");//loopMonth
myXmlTextWriter.WriteElementString("loopDay", null, "loopDay - Do not
remove this element.");//loopDay
myXmlTextWriter.WriteElementString("loopWeek", null, "loopWeek - Do not
remove this element.");//loopWeek
myXmlTextWriter.WriteElementString("loopHour", null, "loopHour - Do not
remove this element.");//loopHour
myXmlTextWriter.WriteElementString("loopMinute", null, "loopMinute - Do
not remove this element.");//loopMinute
myXmlTextWriter.WriteElementString("rid", null, "rid - Do not remove this
element.");//rid
myXmlTextWriter.WriteElementString("lastExecutionTime", null,
"lastExecutionTime - Do not remove this element.");//lastExecutionTime
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.WriteEndElement();
myXmlTextWriter.Flush();
myXmlTextWriter.Close();
}
public bool AddRow(string[] rowData)
{
XmlDataDocument datadoc = new XmlDataDocument();
StreamReader XmlStreamReader = null;
StreamWriter XmlStreamWriter = null;
try
{
XmlStreamReader = new StreamReader(xmlFileName);
datadoc.DataSet.ReadXml(XmlStreamReader, XmlReadMode.InferSchema);
}
catch (Exception ex)
{
return false;
}
finally
{
XmlStreamReader.Close();
}
DataSet ds1 = datadoc.DataSet;
DataRow dr1 = ds1.Tables["scheduleWork"].NewRow();
dr1["workNameSpace"] = rowData[1].Clone().ToString();
dr1["workName"] = rowData[2].Clone().ToString();
dr1["workParameter"] = rowData[3].Clone().ToString();
dr1["workStatus"] = rowData[4].Clone().ToString();
dr1["workStartTime"] = rowData[5].Clone().ToString();
dr1["workEndTime"] = rowData[6].Clone().ToString();
dr1["loopMonth"] = rowData[7].Clone().ToString();
dr1["loopDay"] = rowData[8].Clone().ToString();
dr1["loopWeek"] = rowData[9].Clone().ToString();
dr1["loopHour"] = rowData[10].Clone().ToString();
dr1["loopMinute"] = rowData[11].Clone().ToString();
dr1["rid"] = rowData[12].Clone().ToString();
dr1["lastExecutionTime"] = rowData[13].Clone().ToString();
ds1.Tables["scheduleWork"].Rows.Add(dr1);
try
{
XmlStreamWriter = new StreamWriter(xmlFileName);
datadoc.DataSet.WriteXml(XmlStreamWriter, XmlWriteMode.WriteSchema);
}
catch (Exception ex)
{
return false;
}
finally
{
XmlStreamWriter.Close();
}
return true;
}//AddRow
}//class
Šb Web Applicationªº©I¥sµ{Š¡œX¬°
SchedulerXmlUtil sxu = new SchedulerXmlUtil(scheduleFile);
string[] xmlDataArray = new string[14];
dr[0] = "<a href='javascript:loadRow("+(rowCount+1)+");'>žü€J</a>";
dr[1] = "<input type='submit' name='DelButton' value='§R°£'
id='DelButton"+(rowCount+1)+"' onclick='deleteRow("+(rowCount+1)+");'/>";
dr[2] = workNameSpace.Text;//©RŠWªÅ¶¡
dr[4] = workParameter.Text;//°ÑŒÆ
dr[5] = "°±¥Î";//ª¬ºA
dr[6] = workStartTime.ToString();//¶}©l®É¶¡
dr[7] = workEndTime.ToString();//¥¢®Ä®É¶¡
dr[8] = getLoopTime(loopMonth2, selectAllMonth);//Ž`Àô
dr[9] = getLoopTime(loopDay2, selectAllDay);//Ž`Àô
dr[10] = getLoopTime(loopWeek2, selectAllWeek);//Ž`Àô
dr[11] = getLoopTime(loopHour2, selectAllHour);//Ž`Àô
dr[12] = getLoopTime(loopMinute2, selectAllMinute);//Ž`Àô
dr[13] = "<input type='submit' name='changeStatusButton' value='±Ò¥Î'
id='changeStatusButton"+(rowCount+1)+"'
onclick='changeRowStatus("+(rowCount+1)+");'/>";;//status
sxu.AddRow(xmlDataArray);
¥u­n°õŠæšì©Ò¥s¥Îªºµ{Š¡œX¡AŽN·|¥X²{¥H€U¿ù»~¡G
šÃ¥Œ±Nª«¥ó°ÑŠÒ³]©w¬°ª«¥óªº°õŠæ­ÓÅé
Žy­z: Šb°õŠæ¥Ø«e Web ­nšDªº¹Lµ{€€µo¥Í¥Œ³B²zªºšÒ¥~±¡§Î¡CœÐÀËŸ\°ïÅ|°lÂÜ¥Hšú±o¿ù»~ªºžÔ²Óžê°T¡A¥H€ÎŠbµ{Š¡œX€€²£¥ÍªºŠìžm¡C
šÒ¥~žÔ²Óžê°T: System.NullReferenceException: šÃ¥Œ±Nª«¥ó°ÑŠÒ³]©w¬°ª«¥óªº°õŠæ­ÓÅé
µM«á¿ù»~Šæ«üšìAddRow()€ºªº
[NullReferenceException: šÃ¥Œ±Nª«¥ó°ÑŠÒ³]©w¬°ª«¥óªº°õŠæ­ÓÅé]
WA_setSchedule_onC.SchedulerXmlUtil.AddRow(String[] rowData) in
c:\inetpub\wwwroot\wa_setschedule_onc\schedulerxmlutil.cs:174
WA_setSchedule_onC.WebForm1.Button1_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\wa_setschedule_onc\webform1.aspx.cs:431
System.Web.UI.WebControls.Button.OnClick(EventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String
eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()
œÐ°Ý¡AŠ³¥i¯à¬OšºÃä¥X€F°ÝÃD©O¡H
ÁÂÁÂ
Loading...