简体   繁体   中英

Modifying XML elements with c#

So, I have a problem to modify my XML-document. Here is my XML

<CookBook>
  <Recipe id="1">
    <Title>Best recipe</Title>
    <Category>...</Category>
    <Description>Some text</Description>
    <Amount>10</Amount>
    <Ingredient>
      <li>Ingredient 1</li>
      <li>Ingredient 2</li>
    </Ingredient>
    <RecipeText>
      <li>Step 1</li>
      <li>Step 2</li>
      <li>Step 3</li>
    </RecipeText>
  </Recipe>
  <Recipe id="2">
    <Title>Best recipe2</Title>
    <Category>...</Category>
    <Description>Some text</Description>
    <Amount>10</Amount>
    <Ingredient>
      <li>Ingredient 1</li>
      <li>Ingredient 2</li>
      <li>Ingredient 3</li>
    </Ingredient>
    <RecipeText>
      <li>heat the oven</li>
      <li>Do something</li>
      <li>Do something</li>
    </RecipeText>
  </Recipe>
 </CookBook>

So I need to update specific recipe's li elements which are inside of Ingredient element. But I really don't know how... I have an ingredients list, which contains values that I want to my XML.

foreach (var item in lvAddIngredient.Items)
      {
          string text = item.ToString();
          ingredients.Add(text);
       } 

When I create a new Recipe I use this code

var doc = XDocument.Load("recipeXML.xml");
            var newElement = new XElement("Recipe", new XAttribute("id", id.ToString()),
                new XElement("Title", txtTitle.Text),
                new XElement("Category", selectedCategory.ToString()),
                new XElement("Description", txtDescription.Text),
                new XElement("Amount", txtAmount.Text),
                new XElement("Ingredient", ingredients.Select(text => new XElement("li", text))),
                new XElement("RecipeText", recipeText.Select(text => new XElement("li", text))));
            doc.Element("CookBook").Add(newElement);
            doc.Save("recipeXML.xml");

But I don't know how to update these li elements value. I have tried something like this, but the syntax is wrong.

var xdoc = XDocument.Load("recipeXML.xml");
string id = lbRecipes.SelectedValue.ToString();
var items = from item in xdoc.Descendants("Recipe")
            where item.Attribute("id").Value == id
            select item;
foreach (XElement ele in items)
{
    ele.SetElementValue("Title", txtTitle.Text);
    ele.SetElementValue("Category",cbAddCategory.Text);
    ele.SetElementValue("Amount", txtAmount.Text);
    ele.SetElementValue("Description", txtDescription.Text);
    ele.SetElementValue("Ingredient", ingredients.Select(text => ele.SetElementValue("li", text)));
 }

只需使用您的行标识符“li”即可获得循环所需的内容。

I suggest you to create a tree view for the XML, with the help of the tree view you can browse through the elements existing in the XML, select any value that you would like to update, serialize the tree view after updating the value and save it back to the XML file.

You can create treeview with the help of this snippet!

 try
        {
            treeView1.Nodes.Clear();
            treeView1.Nodes.Add(new TreeNode(doc.DocumentElement.Name));
            TreeNode tNode = new TreeNode();
            tNode = treeView1.Nodes[0];
            AddNode(doc.DocumentElement, tNode);
            treeView1.Show();
        }
        catch (XmlException xmlEx)
        {
            MessageBox.Show(xmlEx.Message);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void AddNode(XmlNode inXmlNode, TreeNode inTreeNode)
    {
        XmlNode xNode;
        TreeNode tNode;
        XmlNodeList nodeList;
        int i;
        if (inXmlNode.HasChildNodes)
        {
            nodeList = inXmlNode.ChildNodes;
            for (i = 0; i <= nodeList.Count - 1; i++)
            {
                xNode = inXmlNode.ChildNodes[i];
                inTreeNode.Nodes.Add(new TreeNode(xNode.Name));
                tNode = inTreeNode.Nodes[i];
                AddNode(xNode, tNode);
            }
        }
        else
        {
            inTreeNode.Text = (inXmlNode.InnerText).Trim();
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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