我正在尝试写入 .xml 文件并收到错误对象引用未设置为对象的实例。在第 49 行(CreateNode)上。这是我尝试过的代码,但没有运气。
private void CreateNode(string Port, string BaudRate, string DataBits, string Parity,_
string StopBits, string Handshaking, XmlTextWriter writer)
{
//Writing to the .xml file. This will make the program be able to load the properties last used.
writer.WriteStartElement("ApplicationProperties");
writer.WriteStartElement("Port");
writer.WriteString(Port);
writer.WriteEndElement();
writer.WriteStartElement("BaudRate");
writer.WriteString(BaudRate);
writer.WriteEndElement();
writer.WriteStartElement("DataBits");
writer.WriteString(DataBits);
writer.WriteEndElement();
writer.WriteStartElement("Parity");
writer.WriteString(Parity);
writer.WriteEndElement();
writer.WriteStartElement("StopBits");
writer.WriteString(StopBits);
writer.WriteEndElement();
writer.WriteStartElement("Handshaking");
writer.WriteString(Handshaking);
writer.WriteEndElement();
writer.WriteEndElement();
}
private void SaveProperties()
{
//CreateNode(everything being referenced. Put text boxes, and drop down boxes here.
XmlTextWriter writer = new XmlTextWriter(@"C:\ForteSenderv2.0\Properties.xml", System.Text.Encoding.UTF8);
writer.WriteStartDocument(true);
//Making the code indeted by 2 characters.
writer.Formatting = Formatting.Indented;
writer.Indentation = 2;
//Making the start element "Table".
writer.WriteStartElement("Forte_Data_Gatherer_Application");
//Calling the rst of the .xml file to write.
CreateNode(ApplicationPort.PortName, ApplicationPort.BaudRate.ToString(), ApplicationPort.DataBits.ToString(), ApplicationPort.Parity.ToString(), ApplicationPort.StopBits.ToString(), ApplicationPort.Handshake.ToString(), writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}