简体   繁体   中英

Read and Write XML in C# using Linq

I want to read and edit the xml file through code. How can I achieve this using XML and Linq? Please help, my xml coding skills are not good.

I want to get the Entity with Name 'new_test' and read/edit the RibbonDiffXml content in that node.

Example 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. what I am doing wrong ? how can i add elements to the RibbonDiffXml element and save?

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'

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:

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.

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