使用 Linq 使用 C# 更新 XML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1487653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:00:45  来源:igfitidea点击:

Update XML with C# using Linq

c#xmllinq

提问by

MY XML FILE STRUCTURE

我的 XML 文件结构

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

TO READ DATA FROM XML BY ITEMID

按 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)
{
     ....
}

How to update XML data by itemID? Thanks!

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

回答by Jon Skeet

Your query is projecting to an anonymous type. If you want to just modify the elements themselves, you want something like:

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

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

Otherwise known as:

否则称为:

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

I suggest you call ToList()as well, so that the whole query is performed and the results stored in a list before you start modifying things:

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

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

回答by Canavar

To update your xml use SetElementValuemethod of the XElement :

要更新您的 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");
}

EDIT :Yes, I tried your example and it saves updated data to the file. Save your updated xml with Save method of the XDocument, here is the code that I tried :

编辑:是的,我试过你的例子,它将更新的数据保存到文件中。使用XDocument 的 Save 方法保存更新的 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");

回答by Rajamohan Anguchamy

To update your xml use element method method of the XElement :

要更新您的 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"));

or

或者

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

you get information form dynamic and update those changes in button click event means, first you checks the Page load following code is present

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

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