I have created a custom configuration section for my App.Config file, right now it looks like this:
<mySection>
<myCollection>
<element name="1" />
<element name="2" />
</myCollection>
</mySection>
As the naming suggests, I have classes inherited from ConfigurationSection (MySection), ConfigurationElementCollection (MyCollection with AddItemName="element" set in the attributes of MySection), and ConfigurationElement. But I wonder how I can realize a structure similar to the following:
<mySection>
<myElements>
<element name="1" />
<element name="2" />
<additionalInfo>"..."</additionalInfo>
</myElements>
<someOtherSetting option="blub" />
</mySection>
Can I still use ConfigurationElementCollection for MyElements or do I have to use something else, as there is not just the element tag (specified by AddItemName="element" in the MySection class), but also another one? If not, what is the class to use here?
And I guess having something between the tags has to just correspond to a property of some AdditionalInfo class, so that it'd be equivalent to the following?
<additionalInfo content="..." />
As for someOtherSetting I guess I could just add another property to MySection corresponding to this, but I didn't get to testing that yet.
Related
Hi I have a xml file that contains
<?xml version="1.0" encoding="utf-8"?>
<resources>
<iconback img1="iconback" />
<iconmask img1="iconmask" />
<iconupon img1="iconupon" />
<scale factor="0.7" />
</resources>
How can I change and save the img="iconback" outer text and Scale factor="0.7?
Everything I try fails with
Cannot set a value on node type 'Element'
I'm creating a logbook application that allows you to create data fields for logbook entries and uses an ObjectListView to display the data. However, this presents a problem. Since field names can change from logbook to logbook, I can't write a single class to deal with the presentation of logbooks.
The application will use an XML format like this:
<entry date="2/18/2014">
<field name="hours" type="int" value="3" display="Hours Used"/>
<field name="gallons" type="double" value="6.8" display="Gallons Used"/>
<filed name="comment" type="string" value="..." display="Comment"/>
</entry>
How can I turn an XML file like the above into data that an ObjectListView can display?
I'm working on highlighting a small scripting language (that is not a derivative of any of the built in's) and I had a custom XML file setup for different syntax keywords and such.
I would really like to use the XSHD system that exists in AvalonEdit for loading custom keywords, but I need to have custom attributes in them.
So basically, after a lot of searching through the source code, I could not really pin-point the file that reads and specifies the keywords and rulesets.
Does anyone here know where I can edit the needed class to read some custom attributes and storing them?
I have all the XSHD and higlighting stuff in AvalonEdit down and working, so it's not using it that is the problem, per say, it's more the problem of using custom attributes in the keywords collection of the XSHD file.
In example:
<Keywords>
<Word defaultValue="hello" requiresShader="shadertype1, shadertype2" someCustomAttr="value">wordname</word>
</Keyword>
So, I need to set some custom properties to the parser of the XSHD file to load these custom attributes and store them in a custom class I have made that handles a few logical components that are crucial to what I'm trying to do.
I already have a working parser that parses a XML file of all my keywords and their properties (but I wrote it to ScintillaNET, but found AvalonEdit afterwards, that works better with WPF and such).
I will edit the original post with this information, sorry about possible misinformation.
Thank you
To load an XSHD file, you need to import the following libraries:
using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Highlighting.Xshd;
And then wherever you create the text editor (omit the first line and just reference the TextEditor if you create it in XAML), use the following code to load def.xshd.
// TextEditor is an AvalonEdit.TextEditor
TextEditor edit = new TextEditor();
XmlReader reader = XmlReader.Create("def.xshd");
edit.SyntaxHighlighting = HighlightingLoader.Load(reader, HighlightingManager.Instance);
A good sample xshd file that I am using for a language in the works is this:
<SyntaxDefinition name="XAPL"
xmlns="http://icsharpcode.net/sharpdevelop/syntaxdefinition/2008">
<Color name="Comment" foreground="Green" />
<Color name="String" foreground="Pink" />
<Color name="Xml" foreground="Orange" />
<!-- 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>
<Span color="Xml" multiline="false">
<Begin>spaz</Begin>
<End>spaz</End>
</Span>
<Keywords fontWeight="bold" foreground="Blue">
<Word>dealwith</Word>
<Word>import</Word>
<Word>end</Word>
<Word>var</Word>
<Word>default</Word>
<Word>template</Word>
<Word>sub</Word>
<Word>category</Word>
<Word>if</Word>
<Word>otherwise</Word>
<Word>and</Word>
<Word>xor</Word>
<Word>string</Word>
<Word>int</Word>
<Word>convert</Word>
<Word>to</Word>
<Word>escape</Word>
<Word>native</Word>
<Word>loop</Word>
<Word>is</Word>
<Word>to</Word>
<Word>from</Word>
<Word>by</Word>
</Keywords>
<!-- Digits -->
<Rule foreground="DarkGray">
\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>
</RuleSet>
</SyntaxDefinition>
The best way to explain my problem is by a example.
I have a Xml file with some elements in it. When I remove a element, I save the file again and this problem occurs:
Before save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2>
<Element Name="TestElement" ID="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
After save
<?xml version="1.0" encoding="utf-8"?>
<ElementTree>
<Elements1 />
<Elements2 />
<Elements3 />
</ElementTree>D="4efa7cc9-a89a-429b-81f4-b00cde729f24" />
</Elements2>
<Elements3 />
</ElementTree>
What I suspect it is doing: It keeps writing the text untill it has reached the end of the text to be saved and then it stops. It does not remove the text that is still writen in the file AFTER the last character in the stream.
How can I fix this?
Make sure you replace the existing file when opening it and you close the stream correctly.
I'm trying to bind to some XML data from my WPF application. I've set up the data context so that the XmlElement I'm trying to bind to ends up looking like this:
<Item name="Potion" classes="Healing Item" value="200">
<Classes>
<Class value="HealingItem" />
</Classes>
<Description value="A low quality potion, it restores a small amount of health" />
<Components>
<HealingComponent>
<BattleHealingComponent>
<HPHealingComponent value="500" type="Absolute"/>
</BattleHealingComponent>
</HealingComponent>
</Components>
</Item>
Now here's the problem. I can't figure out an XPath query I can bind with that will return only the Component Subnodes.
I know it'll go something like this:
ItemsSource="{Binding XPath=Components/*/????}"
I'm stuck on what to use for ????
The result of this query should display "HealingComponent" I've tried playing around with various different parameters on an online XPath visualizer, but I can't figure this one out. I ready about name(), but I can't seem to get that to work.
Any help would be appreciated
In addition to the ItemsSource you probably need an ItemTemplate, this should work:
<ListBox ItemsSource="{Binding XPath=Components/*}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ListBox>
If you do not intend to do anything fancy you can also use the DisplayMemberPath, though in this case the binding ensures that Name is not interpreted as an XPath, you may not have that kind of control with DisplayMemberPath.