我有一个应用程序,它将一些信息写入XML文件:
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
openedFilePath = saveFileDialog1.FileName;
XmlWriterSettings set = new XmlWriterSettings();
set.Indent = true;
set.NewLineOnAttributes = true;
using (XmlWriter writer = XmlWriter.Create(saveFileDialog1.FileName, set))
{
writer.WriteStartDocument();
writer.WriteStartElement("Tree");
writer.WriteStartElement("languages");
foreach (TabPage page in textTC.TabPages)
{
writer.WriteStartElement("language");
writer.WriteElementString("lan", page.Text);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteStartElement("variables");
foreach (Variable v in Form4.variables)
{
writer.WriteStartElement("variableOut");
writer.WriteElementString("varName", v.varName);
if (v.type == VariableType.BOOL)
writer.WriteElementString("type", "bool");
if (v.type == VariableType.INT)
writer.WriteElementString("type", "int");
writer.WriteElementString("value", v.value);
writer.WriteEndElement();
}
writer.WriteEndElement();
foreach (TabPage page in textTC.TabPages)
{
writer.WriteStartElement(page.Text);
writer.WriteStartElement("phrases");
for (int i = 0; i < dialogElements.Count; i++)
{
writer.WriteStartElement("phrase");
writer.WriteElementString("ID", dialogElements[i].ID);
writer.WriteElementString("indexInBranch", dialogElements[i].indexInBranch.ToString());
writer.WriteElementString("characterName", dialogElements[i].characterName);
writer.WriteElementString("text", dialogElements[i].dialogText);
writer.WriteElementString("choiceText", dialogElements[i].choiceText);
writer.WriteElementString("fileName", dialogElements[i].fileName);
writer.WriteElementString("jumpTo", dialogElements[i].jumpTo.ToString());
writer.WriteElementString("idToJump", dialogElements[i].idToJump);
writer.WriteElementString("soundsFolder", soundsFolder);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteStartElement("elements");
for (int i = 0; i < Form5.operationsOfElements.Count; i++)
{
writer.WriteStartElement("element");
writer.WriteElementString("ID", Form5.operationsOfElements[i].id);
for (int j = 0; j < Form5.operationsOfElements[i].setElems.Count; j++)
{
writer.WriteStartElement("SET");
writer.WriteElementString("variable", Form5.operationsOfElements[i].setElems[j].variable);
writer.WriteElementString("operation", Form5.operationsOfElements[i].setElems[j].operation);
writer.WriteElementString("value", Form5.operationsOfElements[i].setElems[j].value);
writer.WriteEndElement();
}
for (int j = 0; j < Form5.operationsOfElements[i].checkElems.Count; j++)
{
writer.WriteStartElement("CHECK");
writer.WriteElementString("variable", Form5.operationsOfElements[i].checkElems[j].variable);
writer.WriteElementString("condition", Form5.operationsOfElements[i].checkElems[j].condition);
writer.WriteElementString("value", Form5.operationsOfElements[i].checkElems[j].value);
writer.WriteElementString("xmlFile", Form5.operationsOfElements[i].checkElems[j].xmlFile);
writer.WriteEndElement();
}
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
writer.WriteEndDocument();
}
}
最初它工作得很好。但是当我添加更多元素时,它开始给我一个这样的错误:
类型&#39; System.InvalidOperationException&#39;的例外情况发生在System.Xml.dll中但未在用户代码中处理
其他信息:调用EndRootElement时编写StartElement将生成无效的XML文档。为了能够编写fragmentXML,ConformanceLevel应该具有ConformanceLevel.Fragment或ConformanceLevel.Auto的属性。
此行引发异常:writer.WriteStartElement(page.Text);
答案 0 :(得分:0)
我会说你的WriteEndElement调用次数不正确。如果您可以在异常代码中找到该行,这将有助于您找到问题所在。
答案 1 :(得分:0)
方法示例中没有提供的异常处理。添加try except
并使用调试器查看引发异常的位置。
答案 2 :(得分:0)
你错误地结束了每个循环中的语言..它应该出现在循环之外
从第5行开始WriteEndElement();切割它并将其从close.BriteEndDocument();
之前的关闭括号(第3个从下)开出答案 3 :(得分:0)
感谢大家的帮助。我发现了自己的错误。还有两个额外的WriteEndElements();在第3区!