Post: [Tutorial | C#] reading from an XML file
08-02-2015, 04:20 AM #1
Code
Banned
(adsbygoogle = window.adsbygoogle || []).push({}); Well I thought this might help some people, basically this can be used for a variety of things, but today I'm going to use an xml document with login details, beware though, this is defiantly not safe for a login system since anybody can access the file :p. First we want to start a new c# project in visual studios, then we want to add these to the top of the programs code-
    
Using System.Net;
Using System.IO;
Using System.Xml;
Using System.Xml.Linq;


Then after "public form1() { }" we want to add this-

    
XmlDocument Xml = new XmlDocument()


So now we have a new xml document, we can start using it. I'm going to load the xml on a button click, but you can also do it on the form load if you want it to refresh every time it's opened.

So I'm going to add a button to my form (in the designer) and double click it and add this-
     
Xml.Load(https://www.yourwebsite.com/yourxmlfile.xml) //add your xml link here, can also point to an xml file on the users computer

For this example, I'm going to have an xml file written like this-
    
<testxml>
<Login Username="Skyrocket" Key="1234"></Login>
<Login Username="DangeeJ" Key="4321"></Login>
</testxml>

Ok so we've created the xml file and opened it, now we need to get each node attribute, so we add this inside the button aswell-
    
XmlNodeList Login = Xml.GetElementByTagName("Login"); //gets everything inside <login>

Foreach (XmlNode node in Login)
{
String user = node.Attributes["UserName"].Value; //gets the value of username=

String Key =node.Attributes["Key"].Value;



And that's really it, now we just check whether the key & username matches another string, for this example I'm using 2 text boxes, so a basic login page. Add this-
    
If (user == textBox1.Text && Key == textBox2.Text)
{
MessageBox.Show("Logged In");
}

So basically what this does, is checks whether the value of Username and Key equals the same as both text boxes.

Like I said earlier, this is most defiantly a good idea for a login system, since anybody can access the xml file and steal users logins. MySQL etc is a much better alternative for that.

Xml reading has endless possibilities of what can be done with it, things such as custom key input, custom settings etc can be used within a local xml file which the user can edit.

Well that's about it! If I forgot anything, please do notify me since I created this thread on my phone (I'm sure some bits aren't fully c&p'able :p)

A̶n̶ ̶e̶x̶a̶m̶p̶l̶e̶ ̶s̶o̶u̶r̶c̶e̶ ̶w̶i̶l̶l̶ ̶b̶e̶ ̶u̶p̶l̶o̶a̶d̶e̶d̶ ̶s̶o̶o̶n̶ ̶o̶n̶c̶e̶ ̶I̶ ̶g̶e̶t̶ ̶m̶y̶ ̶w̶i̶f̶i̶ ̶b̶a̶c̶k̶ ̶u̶p̶ ̶a̶n̶d̶ ̶r̶u̶n̶n̶i̶n̶g̶,̶ ̶b̶u̶t̶ ̶t̶h̶a̶t̶ ̶s̶h̶o̶u̶l̶d̶ ̶b̶e̶ ̶w̶i̶t̶h̶i̶n̶ ̶t̶h̶e̶ ̶c̶o̶m̶i̶n̶g̶ ̶d̶a̶y̶s̶

finally got around to uploading a source Needa


You must login or register to view this content.

You must login or register to view this content.
Last edited by Code ; 08-08-2015 at 07:43 AM.

The following 2 users say thank you to Code for this useful post:

Chen Madhala, SQUID-EYE
08-02-2015, 07:47 PM #2
divxea
Haxor!
Source please
08-03-2015, 03:28 AM #3
Default Avatar
Oneup
Guest
Originally posted by divxea View Post
Source please


Pretty sure that is what he just posted.
08-03-2015, 03:24 PM #4
Ali
Can’t trickshot me!
Originally posted by skyrockett View Post
Well I thought this might help some people, basically this can be used for a variety of things, but today I'm going to use an xml document with login details, beware though, this is defiantly not safe for a login system since anybody can access the file :p. First we want to start a new c# project in visual studios, then we want to add these to the top of the programs code-
    
Using System.Net;
Using System.IO;
Using System.Xml;
Using System.Xml.Linq;


Then after "public form1() { }" we want to add this-

    
XmlDocument Xml = new XmlDocument()


So now we have a new xml document, we can start using it. I'm going to load the xml on a button click, but you can also do it on the form load if you want it to refresh every time it's opened.

So I'm going to add a button to my form (in the designer) and double click it and add this-
     
Xml.Load(https://www.yourwebsite.com/yourxmlfile.xml) //add your xml link here, can also point to an xml file on the users computer

For this example, I'm going to have an xml file written like this-
    
<testxml>
<Login Username="Skyrocket" Key="1234"></Login>
<Login Username="DangeeJ" Key="4321"></Login>
</testxml>

Ok so we've created the xml file and opened it, now we need to get each node attribute, so we add this inside the button aswell-
    
XmlNodeList Login = Xml.GetElementByTagName("Login"); //gets everything inside <login>

Foreach (XmlNode node in Login)
{
String user = node.Attributes["UserName"].Value; //gets the value of username=

String Key =node.Attributes["Key"].Value;



And that's really it, now we just check whether the key & username matches another string, for this example I'm using 2 text boxes, so a basic login page. Add this-
    
If (user == textBox1.Text && Key == textBox2.Text)
{
MessageBox.Show("Logged In");
}

So basically what this does, is checks whether the value of Username and Key equals the same as both text boxes.

Like I said earlier, this is most defiantly a good idea for a login system, since anybody can access the xml file and steal users logins. MySQL etc is a much better alternative for that.

Xml reading has endless possibilities of what can be done with it, things such as custom key input, custom settings etc can be used within a local xml file which the user can edit.

Well that's about it! If I forgot anything, please do notify me since I created this thread on my phone (I'm sure some bits aren't fully c&p'able :p)

An example source will be uploaded soon once I get my wifi back up and running, but that should be within the coming days Happy


Thanks Skyrockett, always a post to help us beginners xD
08-08-2015, 07:36 AM #5
Code
Banned
Heres an example project which reads a key and a name from an xml file and checks it Kryptus

You must login or register to view this content.

You must login or register to view this content.

images-

You must login or register to view this content.

You must login or register to view this content.

You must login or register to view this content.

Last edited by Code ; 08-08-2015 at 07:40 AM.

The following user thanked Code for this useful post:

Ciri
08-10-2015, 07:26 PM #6
Thanks Kryptus

Copyright © 2024, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo