Solved

How to protect xml files?

Posted on 2011-03-22
6
525 Views
Last Modified: 2012-05-11
Hello,

I'm trying to secure my xml files my making them read only and also if possible not to allow users to open them. Below is a code I found in the web but I can't use it with the method that I'm usimg to view my xml using VB.NET is there a way to modify the code to work with my VB.NET project?

Methos used to open xml files:
Dim dt As New DataTable()
dt.Columns.Add("Link_ID")
Dim linker As XElement = XElement.Load(Application.StartupPath + "\LinkFinal.xml")
For Each item As XElement In linker.Elements("Row")
Dim linkID As String = item.Element("Link_ID").Value
 Dim dr As DataRow = dt.NewRow()
            dr("Link_ID") = linkID
Next
 bs.DataSource = dt
        C1TrueDBGrid1.DataSource = bs
        Dim FilteredDT As DataTable

        Dim DV As New DataView(dt, SearchCriteria, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        C1TrueDBGrid1.DataSource = FilteredDT
             
Code found:
File.Open("C:\temp\Filename.xml",FileMode.Append,FileAccess.ReadWrite,FileShare.None);

Thanks,

Victor
0
Comment
Question by:vcharles
6 Comments
 
LVL 9

Expert Comment

by:Hawkvalley1
ID: 35192106
This is something I whipped up awhile back, I would upgrade the encryption part, but the concept is the same.
Encrypt xml.
0
 
LVL 35

Expert Comment

by:Miguel Oz
ID: 35194454
Use:
Dim lStream As New FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None)
Dim doc As As New XmlDocument()
doc.Load(lStream)
Then use doc to manipulate xml data
0
 

Author Comment

by:vcharles
ID: 35195153
Hello,

How do you incorporate the code in part a with my code in part b, still can't figure it out.

Thanks,

Victor

a) Dim lStream As New IO.FileStream(fileName, IO.FileMode.OpenOrCreate, IO.FileAccess.ReadWrite, IO.FileShare.None)
Dim doc As New XmlDocument()
doc.Load(lStream)


b) Dim dt As New DataTable()
dt.Columns.Add("Link_ID")
Dim linker As XElement = XElement.Load(Application.StartupPath + "\LinkFinal.xml")
For Each item As XElement In linker.Elements("Row")
Dim linkID As String = item.Element("Link_ID").Value
 Dim dr As DataRow = dt.NewRow()
            dr("Link_ID") = linkID
Next
 bs.DataSource = dt
        C1TrueDBGrid1.DataSource = bs
        Dim FilteredDT As DataTable

        Dim DV As New DataView(dt, SearchCriteria, Nothing, DataViewRowState.CurrentRows)
        FilteredDT = DV.ToTable
        C1TrueDBGrid1.DataSource = FilteredDT
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 23

Accepted Solution

by:
OP_Zaharin earned 500 total points
ID: 35195270
hi vcharles,
- you can use System.IO.FileAttributes to set the files to readonly as follows:

Dim attribute as System.IO.FileAttributes = FileAttributes.ReadOnly
File.SetAttributes(Application.StartupPath + "\LinkFinal.xml", attribute)


- more on System.IO.FileAttributes:
http://msdn.microsoft.com/en-us/library/system.io.file.setattributes.aspx

- additional samples on setting files attributes (hidden, readonly etc):
http://msdn.microsoft.com/en-us/library/system.io.aspx
0
 

Author Closing Comment

by:vcharles
ID: 35195477
Thank You!
0
 

Author Comment

by:vcharles
ID: 35195491
When you you use the hidden attribute, where is the file hidden?, can users still somehow see it? Can you use more than one attribute to a file?
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

790 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question