使用Linq在C#中读写XML

时间:2014-04-17 06:35:07

标签: c# xml linq

我想通过代码阅读和编辑xml文件。如何使用XML和Linq实现这一目标? 请帮忙,我的xml编码技巧不好。

我想通过名称'new_test'获取实体并读取/编辑该节点中的 RibbonDiffXml 内容。

示例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);

我尝试过这样的事情并没有将我的更改保存到文件中。但是,如果我使用action.Remove(),则保存更改。我做错了什么?如何将元素添加到RibbonDiffXml元素并保存?

xml

2 个答案:

答案 0 :(得分:3)

我已将您的xml放入参考资料中并使用以下代码从实体元素中获取RibbonDiffXml-Element,其中名称为&#39; new_test&#39;

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

要更新您可以使用的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";
                }

要保存更改,您必须致电:

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

答案 1 :(得分:0)

我对此进行了测试并且有效

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

您需要保存xdoc。