使用c#修改XML元素

时间:2016-04-03 15:29:45

标签: c# xml wpf linq ado.net

因此,修改XML文档时遇到问题。这是我的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>

所以我需要更新Ingredient元素中特定配方的li元素。但我真的不知道如何...我有一个成分列表,其中包含我想要的XML值。

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

当我创建新配方时,我使用此代码

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");

但我不知道如何更新这些li元素值。我尝试过类似的东西,但语法错误。

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)));
 }

2 个答案:

答案 0 :(得分:0)

只需使用您的行标识符&#34; li&#34;通过循环获得所需的东西。

答案 1 :(得分:0)

我建议你为XML创建一个树视图,在树视图的帮助下,你可以浏览XML中现有的元素,选择你想要更新的任何值,在更新之后序列化树视图将值保存回XML文件。

您可以借助此代码段创建树视图!

 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();
        }