Solved

How to protect xml files?

Posted on 2011-03-22
6
534 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 36

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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

685 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