Solved

How to protect xml files?

Posted on 2011-03-22
6
491 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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.
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now