如何将 XElements 动态添加到 XML 文件?
how can I dynamically add XElements to an XML file?
好的,所以我的程序将在运行时保存用户从 NumericUpDown 控件提供的任意数量的 XElement。我用XMLtextWriter写的方法如下:
public void XMLWriteNewL4Module()
{
int i;
String workingDir = Directory.GetCurrentDirectory();
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
//opens the document
textWriter.WriteStartDocument(); //Document Start
//first student
textWriter.WriteStartElement("Module"); //Module start
//Module title element
textWriter.WriteStartElement("Title", ""); //Title start
textWriter.WriteString(this.textBoxModuleTitle.Text);
textWriter.WriteEndElement(); //Title end
//Module code element
textWriter.WriteStartElement("Code", ""); //Code start
textWriter.WriteString(this.textBoxModuleCode.Text);
textWriter.WriteEndElement(); //Code end
//Credit value element
textWriter.WriteStartElement("Credit Value", ""); //Credit value start
textWriter.WriteString(this.textBoxModuleCreditValue.Text);
textWriter.WriteEndElement(); //Credit value end
//Assessments element
textWriter.WriteStartElement("Assessments", ""); //Assessments start
for (i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
textWriter.WriteStartElement("Assessment", ""); //Assessment start
textWriter.WriteStartElement("Assessment Type", ""); //Assessment type start
textWriter.WriteString((comboBoxAssessments[i] as ComboBox).Text);
textWriter.WriteEndElement(); //Assessment type end
textWriter.WriteStartElement("Assessment Grade", ""); //Assessment grade start
textWriter.WriteString((textBoxAssessments[i] as TextBox).Text);
textWriter.WriteEndElement(); //Assessment grade end
textWriter.WriteStartElement("Assessment Weighting", ""); //Assessment weighting start
textWriter.WriteString((assessmentWeightingTextBox[i] as TextBox).Text);
textWriter.WriteEndElement(); //Assessment weighting end
textWriter.WriteEndElement(); //Assessment end
}
textWriter.WriteEndElement(); //Assessments end
textWriter.WriteEndElement(); //Module end
//end the document
textWriter.WriteEndDocument(); //Document end
//close the writer
textWriter.Close();
Console.ReadLine();
}
但这种方式不允许我附加 XML 文件,所以我尝试这样写:
public void XMLWriteNewL4Module()
{
String workingDir = Directory.GetCurrentDirectory();
int i;
if (File.Exists(workingDir + @"Level4Modules.xml") == false)
{
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
textWriter.WriteStartElement("Modules");
textWriter.WriteEndElement();
textWriter.Close();
}
XElement xDoc = XElement.Load(workingDir + @"Level4Modules.xml");
xDoc.Add(
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments",
for (i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text),
new XElement("Assessment Grade",),
new XElement("Assessment Weighting",))
}
));
xDoc.Save(workingDir + @"\Students.xml");
}
这不允许我在新的 XElement 声明中使用 for 循环(这是可以理解的),所以我需要一种解决方法,因为我完全被难住了。它显示的错误消息显示 "invalid expression term 'for' " 因为显然 c# 不喜欢您在 XElement 内启动 for 循环。
我有什么办法可以解决这个问题吗?
您可以将 for 循环放入一个新方法中,即 returns XElement[],并在需要子项的地方调用该方法。
像这样:
C#
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments", GetAssessments())); //the returned items will be added as child items to the "Assessments" node
C#
XElement[] GetAssessments()
{
var result = new List<XElement>();
for (int i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
result.Add(new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text));
result.Add(new XElement("Assessment Grade", ""));
result.Add(new XElement("Assessment Weighting", ""));
}
return result.ToArray();
}
如果有一些上下文信息,您可以将其作为参数传递到新方法中。
希望能帮到你。
您可以使用 Enumerable.Range
扩展,它应该允许您在 XElement
.
中构建嵌套元素
public void XMLWriteNewL4Module()
{
String workingDir = Directory.GetCurrentDirectory();
int i;
if (File.Exists(workingDir + @"Level4Modules.xml") == false)
{
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
textWriter.WriteStartElement("Modules");
textWriter.WriteEndElement();
textWriter.Close();
}
XElement xDoc = XElement.Load(workingDir + @"Level4Modules.xml");
xDoc.Add(
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments", Enumerable.Range(0,numericUpDownNoOfAssessments.Value)
.Select(i=>
{
return new XElement[] {
new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text),
new XElement("Assessment Grade"),
new XElement("Assessment Weighting")
};
}))));
xDoc.Save(workingDir + @"\Students.xml");
}
好的,所以我的程序将在运行时保存用户从 NumericUpDown 控件提供的任意数量的 XElement。我用XMLtextWriter写的方法如下:
public void XMLWriteNewL4Module()
{
int i;
String workingDir = Directory.GetCurrentDirectory();
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
//opens the document
textWriter.WriteStartDocument(); //Document Start
//first student
textWriter.WriteStartElement("Module"); //Module start
//Module title element
textWriter.WriteStartElement("Title", ""); //Title start
textWriter.WriteString(this.textBoxModuleTitle.Text);
textWriter.WriteEndElement(); //Title end
//Module code element
textWriter.WriteStartElement("Code", ""); //Code start
textWriter.WriteString(this.textBoxModuleCode.Text);
textWriter.WriteEndElement(); //Code end
//Credit value element
textWriter.WriteStartElement("Credit Value", ""); //Credit value start
textWriter.WriteString(this.textBoxModuleCreditValue.Text);
textWriter.WriteEndElement(); //Credit value end
//Assessments element
textWriter.WriteStartElement("Assessments", ""); //Assessments start
for (i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
textWriter.WriteStartElement("Assessment", ""); //Assessment start
textWriter.WriteStartElement("Assessment Type", ""); //Assessment type start
textWriter.WriteString((comboBoxAssessments[i] as ComboBox).Text);
textWriter.WriteEndElement(); //Assessment type end
textWriter.WriteStartElement("Assessment Grade", ""); //Assessment grade start
textWriter.WriteString((textBoxAssessments[i] as TextBox).Text);
textWriter.WriteEndElement(); //Assessment grade end
textWriter.WriteStartElement("Assessment Weighting", ""); //Assessment weighting start
textWriter.WriteString((assessmentWeightingTextBox[i] as TextBox).Text);
textWriter.WriteEndElement(); //Assessment weighting end
textWriter.WriteEndElement(); //Assessment end
}
textWriter.WriteEndElement(); //Assessments end
textWriter.WriteEndElement(); //Module end
//end the document
textWriter.WriteEndDocument(); //Document end
//close the writer
textWriter.Close();
Console.ReadLine();
}
但这种方式不允许我附加 XML 文件,所以我尝试这样写:
public void XMLWriteNewL4Module()
{
String workingDir = Directory.GetCurrentDirectory();
int i;
if (File.Exists(workingDir + @"Level4Modules.xml") == false)
{
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
textWriter.WriteStartElement("Modules");
textWriter.WriteEndElement();
textWriter.Close();
}
XElement xDoc = XElement.Load(workingDir + @"Level4Modules.xml");
xDoc.Add(
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments",
for (i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text),
new XElement("Assessment Grade",),
new XElement("Assessment Weighting",))
}
));
xDoc.Save(workingDir + @"\Students.xml");
}
这不允许我在新的 XElement 声明中使用 for 循环(这是可以理解的),所以我需要一种解决方法,因为我完全被难住了。它显示的错误消息显示 "invalid expression term 'for' " 因为显然 c# 不喜欢您在 XElement 内启动 for 循环。 我有什么办法可以解决这个问题吗?
您可以将 for 循环放入一个新方法中,即 returns XElement[],并在需要子项的地方调用该方法。 像这样:
C#
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments", GetAssessments())); //the returned items will be added as child items to the "Assessments" node
C#
XElement[] GetAssessments()
{
var result = new List<XElement>();
for (int i = 0; i < numericUpDownNoOfAssessments.Value; i++)
{
result.Add(new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text));
result.Add(new XElement("Assessment Grade", ""));
result.Add(new XElement("Assessment Weighting", ""));
}
return result.ToArray();
}
如果有一些上下文信息,您可以将其作为参数传递到新方法中。 希望能帮到你。
您可以使用 Enumerable.Range
扩展,它应该允许您在 XElement
.
public void XMLWriteNewL4Module()
{
String workingDir = Directory.GetCurrentDirectory();
int i;
if (File.Exists(workingDir + @"Level4Modules.xml") == false)
{
//create a new file in the working directory
XmlTextWriter textWriter = new XmlTextWriter("Level4Modules.xml", null);
textWriter.WriteStartElement("Modules");
textWriter.WriteEndElement();
textWriter.Close();
}
XElement xDoc = XElement.Load(workingDir + @"Level4Modules.xml");
xDoc.Add(
new XElement("Module",
new XElement("Title", this.textBoxStudentFirstName.Text),
new XElement("Code", this.textBoxStudentSurname.Text),
new XElement("Credit Value", this.textBoxStudentIDNewUser.Text),
new XElement("Assessments", Enumerable.Range(0,numericUpDownNoOfAssessments.Value)
.Select(i=>
{
return new XElement[] {
new XElement("Assessment Type", (comboBoxAssessments[i] as ComboBox).Text),
new XElement("Assessment Grade"),
new XElement("Assessment Weighting")
};
}))));
xDoc.Save(workingDir + @"\Students.xml");
}