简体   繁体   English

使用Linq在C#中读写XML

[英]Read and Write XML in C# using Linq

I want to read and edit the xml file through code. 我想通过代码阅读和编辑xml文件。 How can I achieve this using XML and Linq? 如何使用XML和Linq实现此目的? Please help, my xml coding skills are not good. 请帮忙,我的xml编码技能不好。

I want to get the Entity with Name 'new_test' and read/edit the RibbonDiffXml content in that node. 我想获取名称为 “ new_test”的实体并读取/编辑该节点中的RibbonDiffXml内容。

Example XML: XML示例:

<ImportExportXml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Entities>
<Entity>
  <Name LocalizedName="test" OriginalName="test">new_test</Name>
  <EntityInfo>...</EntityInfo>
  ..
  <RibbonDiffXml>..</RibbonDiffXml>
  ..
</Entity>
<Entity>
  <Name LocalizedName="Account" OriginalName="Account">account</Name>
  <EntityInfo>..</EntityInfo>
  ... 
</Entity>
</Entities>
</ImportExportXml>

string xml = @"<CustomActions>
                      <CustomAction Id='MsCrm.deal.Form.Clone.CustomAction' Location='Mscrm.Form.deal.MainTab.Save.Controls._children' Sequence='46'>
                        <CommandUIDefinition>
                          <Button Alt='$LocLabels:MsCrm.deal.Form.Clone.Alt' Command='MsCrm.deal.CloneCommand' Id='MsCrm.deal.Form.Clone' Image32by32='$webresource:new_/image/clone32.png' Image16by16='$webresource:new_/image/clone16.png' LabelText='$LocLabels:MsCrm.deal.Form.Clone.LabelText' Sequence='46' TemplateAlias='o1' ToolTipTitle='$LocLabels:MsCrm.deal.Form.Clone.ToolTipTitle' ToolTipDescription='$LocLabels:MsCrm.deal.Form.Clone.ToolTipDescription' />
                        </CommandUIDefinition>
                      </CustomAction>
                    </CustomActions>";  


var ribbon = from entity in document.Root.Element("Entities").Elements()
                         where entity.Element("Name") != null && entity.Element("Name").Value == entityName
                         select entity.Element("RibbonDiffXml");


            var action = ribbon.Elements("CustomActions").ToList();

            action.Add(XElement.Parse(xml));

            document.Save(filePath);

I have tried something like this and it doesn't save my changes to the file. 我已经尝试过类似的操作,但它不会将更改保存到文件中。 However if I use action.Remove() the changes get saved OK. 但是,如果我使用action.Remove(),则更改将保存为确定。 what I am doing wrong ? 我做错了什么? how can i add elements to the RibbonDiffXml element and save? 如何将元素添加到RibbonDiffXml元素并保存?

XML文件

I've putted your xml into the Resources and used the following code to get the RibbonDiffXml-Element from the Entity-Element where the Name is 'new_test' 我将您的xml放入资源中,并使用以下代码从名称为'new_test'的Entity-Element中获取RibbonDiffXml-Element。

XDocument document = XDocument.Parse(Properties.Resources.XML);
if (document.Root != null)
{
    IEnumerable<string> elements = 
    (from entity in document.Root.Element("Entities").Elements()
    let name = entity.Element("Name")
    where name != null && name.Value == "new_test"
    let ribbonDiffXml = entity.Element("RibbonDiffXml")
    where ribbonDiffXml != null
    select ribbonDiffXml.Value);
}

To update the XML you can use: 要更新XML,可以使用:

foreach (XElement ribbonDiffXml in from entity in document.Root.Element("Entities").Elements() 
                                   let name = entity.Element("Name") 
                                   where name != null && name.Value == "new_test" 
                                   select entity.Element("RibbonDiffXml") 
                                   into ribbonDiffXml 
                                   where ribbonDiffXml != null select ribbonDiffXml)
                {
                    ribbonDiffXml.Value = "Changed RibbonDiffXml";
                }

To save your changes you have to call: 要保存更改,您必须致电:

document.Save("PATH OR STREAM OR ...");

I tested this and it worked 我测试了一下,它起作用了

String s = @"<ImportExportXml><Entities><Entity><Name>new_test</Name><EntityInfo></EntityInfo><RibbonDiffXml></RibbonDiffXml></Entity></Entities></ImportExportXml>";

var xdoc = XElement.Parse(s);
var f = (from x in xdoc.DescendantsAndSelf(@"Entities")
        where x.Element("Entity").Element("Name").Value == "new_test"
        select x.Element("Entity").Element("RibbonDiffXml"));

foreach(var y in f)
{
    y.Value = "Hello World";
}

You need to save xdoc. 您需要保存xdoc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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