Solved

How to protect xml files?

Posted on 2011-03-22
6
504 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
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…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

911 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

19 Experts available now in Live!

Get 1:1 Help Now