我正在尝试写入.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();
}
答案 0 :(得分:2)
Dim mySerialPort as serialPort
mySerialPort.PortName = SetPortName(mySerialPort.ApplicationPort);
修改强>
error: Object reference not set to an instance of an object. on line 49 (CreateNode).
private void CreateNode(string Port, string BaudRate, string DataBits, string Parity,_
string StopBits, string Handshaking, XmlTextWriter writer)
private void blabla (PortName as string, BaudRate as string, DataBits as string,_
Parity as string, Handshaking as string)
我看不出你将如何拥有XmlTextwriter。 就个人而言,我没有看到任何类似的东西......我可以看到你的努力,非常感谢你的工作以及你想要实现的目标,我真的认为你过于复杂。编码技巧(不是我是专家)是采用最简单的方法来创建所需的功能。尽可能短,只要有必要。
我认为你需要重新考虑你想要实现的算法,我是以最简单的方式解决这个问题还是我为这个项目做了不必要的复杂化?
When creating subs, functions等,您不需要将所有变量填入顶部的声明中。这就是身体的用途。
我恳请您使用MSDN并对此进行更多研究,这将变得更容易。这是MSDN .Net Framework Class Library的链接 - 它涵盖了所有内容:)