0

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

6
  • accept the answer if it helped you. you'll get reputation too Commented Mar 13, 2011 at 14:08
  • What do you want to update the value to, in the XML? You've given sample input - what do you want the output to be? Commented Mar 13, 2011 at 14:13
  • updata 'aaa' to 'kkk' by linq Commented Mar 13, 2011 at 14:14
  • @beginner: And where does the value "kkk" come from? Commented Mar 13, 2011 at 14:15
  • 1
    @beginner: From your text box? Currently you're only updating the value of your text box, not the XML at all. Could I suggest you read my guide to asking SO questions effectively? tinyurl.com/so-hints Commented Mar 13, 2011 at 14:24

2 Answers 2

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");
Sign up to request clarification or add additional context in comments.

1 Comment

what is doc.Save("changed.xml"); <"changed.xml">
1

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().

8 Comments

tanks . very very very muck . how can updata xml by linq
It's unclear to me why this answer has been accepted, given that it doesn't modify the XML at all...
Hi Jon, the question was different previously. Take a look at the edits ;) Anyway I have updated it with the new requests.
@AS-CII: Yes, I hadn't seen the edits - but your answer still doesn't appear to be modifying the XML.
question.Reserver.Value = "True"; this not work that update Reserver=true
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.