If I understand the question, you want to encrypt just the
values of the XML, not the entire file?
When dealing with XML, I typically use wrapper classes. Create a
BookClass
with a constructor that takes the
XmlElement
containing the data of a single
<book>
node. To illustrate (and my apologies for it being VB):
Public Sub New(ByVal Elem As XmlElement)
For Each N As XmlNode In Elem.ChildNodes
If N.NodeType = XmlNodeType.Element Then
Select Case N.Name
Case "Title"
pTitle = N.InnerText
Case "Author"
pAuthor= N.InnerText
End Select
End If
Next
End Sub
The variables that start with
p
get exposed as properties.
Override
ToString
, or create some other function, to write
BookClass
's data back out as XML. Then, add methods that will encrypt and decrypt the data, using whatever method suits your needs; my toolbox uses the
RijndaelManaged
library in
System.Security.Cryptography
, which produces a string of hex digits that won't interfere with XML. When the node is written, it will not matter whether the data is encrypted or in plain text.
Finally, write methods that will read your file into a
List<BookClass>
and write the list back out as a file. If you wanted to be fancy, you could encapsulate all this into a single
Library
class that inherits from
List<BookClass>
.
Now, your workflow would look like:
1. Read file and return a list or
Library
.
2. For each
BookClass
, call the class'
Decrypt
method.
3. Use the XML.
If you make changes and need to resave the data, just call
Encrypt
for each
BookClass
, then write the list back out.