Search code examples
c#xmltext-formattingavalonedit

AvalonEdit : How to modify the xshd file to change elements atrribute


I try to modify xshd file to change attribute foreground of element Color programmatically in c#. I tried to used XmlAtrribute to have an access to to it and change it but it didnt work. How can i change it ? Thats my xshd file below

 <?xml version="1.0"?>
<SyntaxDefinition name="Boo" extensions=".boo" xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="String" foreground="Red" />
<Color name="Comment" foreground="Green" />

<!-- This is the main ruleset. -->
<RuleSet>
<Span color="Comment" begin="//" />
<Span color="Comment" multiline="true" begin="/\*" end="\*/" />
<Span color="String">
  <Begin>'</Begin>
  <End>'</End>
  <RuleSet>
    <!-- nested span for escape sequences -->
    <Span begin="\\" end="." />
  </RuleSet>
</Span>
<Keywords fontWeight="bold" foreground="Green">
  <Word>IF</Word>
  <Word>IFEND</Word>
  <Word>DECLARED</Word>
</Keywords>
<!-- Digits -->
<Rule foreground="Gray">
    \b0[xX][0-9a-fA-F]+  # hex number
    |    \b
    (    \d+(\.[0-9]+)?   #number with optional floating point
    |    \.[0-9]+         #or just starting with floating point
    )
    ([eE][+-]?[0-9]+)? # optional exponent
  </Rule>
<Rule  foreground="Blue">
    \w*-\w*-*\w*
  </Rule>
<Rule foreground="Pink">
    (\w*=\w*)
  </Rule>
 </RuleSet>
</SyntaxDefinition>

Solution

  • With LINQ try this :

    XDocument doc = XDocument.Load("file.xml");
    XNamespace ns = "http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008";
    var element = doc.Descendants(ns + "Color").First(x => x.Attribute("name").Value == "String");
    element.SetAttributeValue("foreground", "VALUECOLOR");
    doc.Save("file.xml");