Insert new XML node using LINQ

2019-08-23 03:35发布

问题:

XML: 1 aaa 2 bbb

Code

var doc = XDocument.Load (Server.MapPath(".") + "\\Questions.config");
var elements = from element in doc.Descendants("Question")
               select new
               {
                   Id = element.Element("Id").Value,
                   Text = element.Element("Text").Value,
                   Reserver = element.Element("Reserver") != null
               };

StringBuilder builder = new StringBuilder();
foreach (var question in elements)
{
    builder.AppendLine(question.Id + "-" + question.Text);
}
myTextBox.Text = builder.ToString();

how insert new Node 'Question' to XML File

回答1:

It's unclear exactly what your question means, but the basic process of updating the XML file would be along the lines of:

  • Load the XML document into memory, as you're already doing
  • Identify the element you want to change, which will depend on what the criteria are
  • Update it (e.g. setting the Value property to "kkk" as per your comments)
  • Save the XML document using doc.Save("file.xml") or something similar

It's hard to be more precise than that without having more precise requirements. As an example though, if you wanted to prefix every Text node in the document with "Question x: " where x is the ID of the question, you might write something like:

var doc = XDocument.Load("file.xml");
var elements = doc.Descendants("Question");

foreach (var question in elements)
{
    int id = (int) question.Element("ID");
    XElement textElement = question.Element("Text");
    textElement.Value = "Question: " + id + " = " + textElement.Value;
}

doc.Save("changed.xml");

Or to change every "aaa" Text element to "kkk":

var doc = XDocument.Load("file.xml");
var elements = doc.Descendants("Text")
                  .Where(x => x.Value == "aaa");

foreach (var textElement in elements)
{
    textElement.Value = "kkk";
}

doc.Save("changed.xml");


回答2:

Are you trying to do something like this?

var doc = XDocument.Load (Server.MapPath(".") + "\\Questions.config");
var elements = from element in doc.Descendants("Question")
               select new
               {
                   Id = element.Element("Id").Value,
                   Text = element.Element("Text").Value,
                   Reserver = element.Element("Reserver") != null
               };

StringBuilder builder = new StringBuilder();
foreach (var question in elements)
{
    builder.AppendLine(question.Id + "-" + question.Text);
}
myTextBox.Text = builder.ToString();

EDIT: If you want to update every question then you have to slightly modify the code above.

var elements = from element in doc.Descendants("Question")
               select new
               {
                   Id = element.Element("Id"),
                   Text = element.Element("Text"),
                   Reserver = element.Element("Reserver")
               };
StringBuilder builder = new StringBuilder();
foreach (var question in elements)
{
    // Read
    builder.AppendLine(question.Id.Value + "-" + question.Text.Value);

    // Write
    question.Reserver.Value = "True";
}
myTextBox.Text = builder.ToString();

In this way you don't select the value anymore but the XElement instead, so you're able to modify the XML. Remember also to save the file using XDocument.Save().