简体   繁体   English

使用LINQ插入新的XML节点

[英]Insert new XML node using LINQ

XML: 1 aaa 2 bbb 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 如何将新节点“问题”插入XML文件

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. 通过这种方式,您不再选择值,而是选择XElement,因此您可以修改XML。 Remember also to save the file using XDocument.Save() . 还要记住使用XDocument.Save()保存文件。

It's unclear exactly what your question means, but the basic process of updating the XML file would be along the lines of: 目前还不清楚你的问题到底意味着什么,但更新XML文件的基本过程将是:

  • Load the XML document into memory, as you're already doing 正如您所做的那样,将XML文档加载到内存中
  • Identify the element you want to change, which will depend on what the criteria are 确定要更改的元素,具体取决于条件
  • Update it (eg setting the Value property to "kkk" as per your comments) 更新它(例如根据您的评论将Value属性设置为“kkk”)
  • Save the XML document using doc.Save("file.xml") or something similar 使用doc.Save("file.xml")或类似的东西保存XML文档

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: 但是,如果您想为文档中的每个Text节点添加“问题x:”前缀,其中x是问题的ID,您可以编写如下内容:

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": 或者将每个“aaa”Text元素更改为“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");

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

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