简体   繁体   English

使用C#中的另一个XML文档中的元素附加XML文档

[英]Appending an XML Document with elements from another XML Document in C#

I am trying to build an automatic test grading application and I want the end result to be a savefiledialog prompt that allows the grader to save a new xml file to any location. 我正在尝试构建一个自动测试评分应用程序,并且我希望最终结果是一个savefiledialog提示,该提示允许评分者将新的xml文件保存到任何位置。

The trick is I need that output xml to include the original test + appended answers from answer key + final grade. 诀窍是我需要输出xml包含原始测试+答案键的附加答案+最终成绩。 Here is what I have so far but Im getting an error. 这是我到目前为止的内容,但是我遇到了错误。 I am not familiar with .appendchild() so Im thinking that is where my issue lies. 我不熟悉.appendchild(),所以我认为这就是我的问题所在。

    private void cbOutput_Click(object sender, EventArgs e)
    {
        XmlNode rootTest = xmlAnswers.DocumentElement;
        XmlNode rootKey = xmlAnswerKey.DocumentElement;

        XmlNodeList nodeListTest = rootTest.SelectNodes("//answer");

        foreach (XmlNode item in nodeListTest)
        {
            XmlNode importNode = rootTest.OwnerDocument.ImportNode(rootKey, true);

            item.AppendChild(importNode);
        }

        SaveFileDialog savefiledialog1 = new SaveFileDialog();
        saveFileDialog1.Filter = "XML Files|*.XML";
        saveFileDialog1.Title = "Choose Location to Save Graded Test";

        if (saveFileDialog1.ShowDialog() == DialogResult.OK)
        {
            if (!string.IsNullOrEmpty(savefiledialog1.FileName))
            using (Stream s = File.Open(savefiledialog1.FileName, FileMode.Create))
            using (StreamWriter sw = new StreamWriter(s))
            {
                sw.Write(rootTest + tbTotal.Text);
            }
        }

I have a hard time following your code, since it is pre .Net 3.5 XML code. 我很难遵循您的代码,因为它是.Net 3.5 XML之前的代码。 I started in on C# when 3.5 came out. 当3.5发布时,我开始使用C#。 Seems like you are adding the keys to every answer in the test? 好像您要在测试中的每个答案中添加键? Doesn't make sense from what you wrote as your description. 从您写的描述中没有任何意义。 But if that was your goal, you can try this. 但是,如果这是您的目标,则可以尝试一下。

If you can use .Net 3.5 then use XElement / XDocument , using these converters do 如果可以使用.Net 3.5,请使用XElement / XDocument ,使用这些转换器可以

XElement test = xmlAnswers.ToXElement();
XElement keys = xmlAnswerKey.ToXElement();

test.Descendants("answer")
    .ToList()
    .ForEach(a => a.Add(new XElement(keys))); // Add clone of keys to each answer
// Dialog stuff
test.Save(savefiledialog1.FileName);

Or, using your description (I'll have to try and make up what you should be doing, since your XML is hidden from us). 或者,使用您的描述(由于您的XML对我们而言是隐藏的,因此我将不得不尝试弥补您应该做的事情)。

XElement test = xmlAnswers.ToXElement();
XElement keys = xmlAnswerKey.ToXElement();

test.Add(keys.Descendants("answer").ToList()); // Add Answers to test
test.Add(new XElement("finalgrade", "A")); // Add final grade
// Dialog stuff
test.Save(savefiledialog1.FileName);

PS, don't do all the converting, adding of answers etc outside the 附言,不要做所有的转换,添加答案等

if (saveFileDialog1.ShowDialog() == DialogResult.OK)

Otherwise, if it isn't OK then you are wasting the time of your user. 否则,如果不OK那么您就是在浪费用户的时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM