0

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

1
  • 1
    If you want to improve your xml coding skills, maybe LINQ to XML is a good start. Commented Apr 17, 2014 at 6:42

2 Answers 2

3

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 ...");
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you Tom, I'll use this and check.
Hi, the RibbonDiffXml has child nodes and I'm trying to add nodes to it. However my changes are not being saved. 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();
Please update your Question with the code from the comment and the full xml (with the content of RibbonDiffXml)
i have posted the code below. please take a look when you get a chance
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.