Home > database >  Store enumaration value to xml file
Store enumaration value to xml file

Time:10-27

I'm writing a program, that uses the FileSystemWatcher. I want to store the user configuration in a xml file.

The FileSystemWatcher has a property "NotifyFilter" (System.IO.NotifyFilters), which is an enum (see NotifyFilters Enum)

There can be multiple values selected (e.g. "FileName" and "LastWrite").

But I have no idea how to store such a combination as plain text, in my situation to a xml file. Could also be just a text file. And of course read it again and assign the value to the FileSystemWatcher-Object.

I hope, I could describe my problem clearly. Can anyone help me with a hint or some code snippets?

Thank you!

CodePudding user response:

An enumeration is a set of named constants whose underlying type is any integral type (typically an Integer).

That is why in the documentation you see the table that has the named constant in the first column, the integral value in the second column, and then a description in the last column.

If you want to store the value, then convert the enumeration to an Integer using one of the conversion methods. When you want to convert the Integer back to an enumeration, call Enum.Parse (documentation) or Enum.TryParse (documentation).

Here is an example:

Console.WriteLine(Convert.ToInt32(NotifyFilters.CreationTime))
Console.WriteLine([Enum].Parse(GetType(NotifyFilters), "64"))

Fiddle: https://dotnetfiddle.net/ggIsMB

If you need to store multiple values, then I would store the values in a parent node, something along these lines:

<filters>
  <FileName />
  <LastWrite />
  <etc... />
</filters>
<!-- or -->
<filters>
  <enum value="64" />
  <enum value="1" />
  <etc... />
</filters>
  • Related