【问题标题】:Update XML with C# using Linq使用 Linq 使用 C# 更新 XML
【发布时间】:2010-12-02 00:57:18
【问题描述】:

我的 XML 文件结构

<items>
  <item>
    <itemID>1</itemID>
    <isGadget>True</isGadget>
    <name>Star Wars Figures</name>
    <text1>LukeSkywalker</text1>
  </item>
</items>

按ITEMID从XML中读取数据

XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml"));
var items = from item in xmlDoc.Descendants("item")
            where item.Element("itemID").Value == itemID
            select new
            {
                itemID = item.Element("itemID").Value,
                isGadget = bool.Parse(item.Element("isGadget").Value),
                name = item.Element("name").Value,
                text1 = item.Element("text1").Value,
             }

foreach (var item in items)
{
     ....
}

如何通过 itemID 更新 XML 数据? 谢谢!

【问题讨论】:

标签: c# xml linq


【解决方案1】:

要更新您的 xml,请使用 XElement 的 SetElementValue 方法:

var items = from item in xmlDoc.Descendants("item")
    where item.Element("itemID").Value == itemID
    select item;

foreach (XElement itemElement in items)
{
    itemElement.SetElementValue("name", "Lord of the Rings Figures");
}

编辑: 是的,我尝试了您的示例,它将更新的数据保存到文件中。使用Save method of the XDocument 保存更新后的 xml,这是我尝试过的代码:

string xml = @"<items>
           <item>
            <itemID>1</itemID>
            <isGadget>True</isGadget>
            <name>Star Wars Figures</name>
            <text1>LukeSkywalker</text1>
           </item>
        </items>";

XDocument xmlDoc = XDocument.Parse(xml);

var items = from item in xmlDoc.Descendants("item")
            where item.Element("itemID").Value == "1"
            select item;

foreach (XElement itemElement in items)
{
    itemElement.SetElementValue("name", "Lord of the Rings Figures");
}

xmlDoc.Save("data.xml");

【讨论】:

  • 不工作。也许我错过了一些东西。我必须添加 xmlDoc.Save("data.xml") 吗?即使使用 xmlDoc.Save,它仍然不会更新。
  • Canavar,我希望直接更新 xml 文件,而不必先将其转换为字符串。谢谢!
【解决方案2】:

要更新您的 xml 使用 XElement 的元素方法方法:

XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml"));
var items = (from item in xmlDoc.Descendants("item")
            where item.Element("itemID").Value == itemID
            select item).ToList();
         foreach (var item in items)
         {
                item.Element("itemID").Value=NewValue;
                bool.Parse(item.Element("isGadget").Value)=Newvalue;
                item.Element("name").Value=Newvalue;
                item.Element("text1").Value=Newvalue;
         }
xmlDoc.Save(HttpContext.Current.Server.MapPath("data.xml"));

XDocument xmlDoc = XDocument.Load(HttpContext.Current.Server.MapPath("data.xml"));
             foreach (var item in (from item in xmlDoc.Descendants("item")
                where item.Element("itemID").Value == itemID
                select item).ToList())
             {
                    item.Element("itemID").Value=NewValue;
                    bool.Parse(item.Element("isGadget").Value)=Newvalue;
                    item.Element("name").Value=Newvalue;
                    item.Element("text1").Value=Newvalue;
             }
    xmlDoc.Save(HttpContext.Current.Server.MapPath("data.xml"));

您获取动态信息表单并更新按钮单击事件中的这些更改,首先您检查页面加载是否存在以下代码

if(!Page.IsPostBack) { .... } 

【讨论】:

    【解决方案3】:

    您的查询投射到匿名类型。如果您只想修改元素本身,则需要以下内容:

    var items = from item in xmlDoc.Descendants("item")
                where item.Element("itemID").Value == itemID
                select item;
    

    也称为:

    var items = xmlDoc.Descendants("item")
                      .Where(item => item.Element("itemID").Value == itemID);
    

    我建议您也致电ToList(),以便在开始修改之前执行整个查询并将结果存储在列表中:

    var items = xmlDoc.Descendants("item")
                      .Where(item => item.Element("itemID").Value == itemID)
                      .ToList();
    

    【讨论】:

    • 你能提供一个简单的调用 toList() 来更新 xml 的例子吗?是否需要使用 foreach 循环来设置元素值?
    • 调用 ToList() 不会更新 XML,是的,您需要使用 foreach。你还没有说你想如何更新 XML,所以举个例子有点棘手。
    • 不确定更新 XML 文档的选项有哪些。也许您可以提出最有效的方法。 :)
    • 我们真的可以推荐调用 ToList 吗?我见过初级开发人员在每次查询后都需要一个 ToList 或 ToArray,无论使用情况如何。在我看来,应该有更好的使用指南。毕竟,这是使用 List.ForEach() 的途径。冷酷!
    猜你喜欢
    • 2012-09-28
    • 2012-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多