Link to home
Start Free TrialLog in
Avatar of GAM3R
GAM3R

asked on

Reading Online XML File

Hello, I have the code below reading a XML file but how do I make it read a XML hosted on a website? I would like to download it as a string and read it so I can decrypt the info. Then run the method in the code.

OR

Tell me how to read a online .xml file.
XmlTextReader reader = new XmlTextReader(File.Open("your_xml_file", FileMode.Open));
            reader.ReadToDescendant("user");

            bool found = false;
            do
            {
                if (reader["username"] == usernameText.Text && reader["password"] == passwordText.Text)
                {
                    found = true;
                    break;
                }
            } while (reader.ReadToNextSibling("User"));

            reader.Close();

Open in new window

Avatar of trestan
trestan
Flag of Canada image

Avatar of GAM3R
GAM3R

ASKER

When I use my URL http://cheatingzone.net/LoginSystem.xml I get the error Unable to connect to the remote server
just tried, this works in my computer:
XDocument x = XDocument.Load("http://cheatingzone.net/LoginSystem.xml");
not sure why it does work to you.
Avatar of GAM3R

ASKER


public void Check_User(String User, String Pass)
        {
            if (User == txtUsername.Text && Pass == txtPassword.Text)
            {
                this.expandablePanel1.Text = txtUsername.Text;
            }
        }
        private void cmdLogin_Click(object sender, EventArgs e)
        {
            XmlDocument _doc = new XmlDocument();
            _doc.LoadXml("d:\\LoginSystem.xml");
            // alternately, _doc.Load( _strFilename); to read from a file.
            XmlNodeList Username = _doc.GetElementsByTagName("Username");
            XmlNodeList Password = _doc.GetElementsByTagName("Password");
            // I'm assuming every FirstName has a LastName in this example, your requirements may vary. // 
            for (int i = 0; i < Username.Count; ++i)
            {
                Check_User(Username[i].InnerText,
                Password[i].InnerText);
            }
}



<Users>
<User>
<Username>Admin</Username>
<Password>test</Password>
</User>
<User>
<Username>test1</Username>
<Password>test1</Password>
</User>
</Users>

Open in new window

Avatar of GAM3R

ASKER

The error I get is Data at the root level is invalid. Line 1, position 1.
i use this:
using System.Xml.Linq;

and
XDocument x = XDocument.Load("http://cheatingzone.net/LoginSystem.xml");

not
XmlDocument _doc = new XmlDocument();
 _doc.LoadXml("d:\\LoginSystem.xml");
I gave it a try and the codes work just fine for me. I paste the link to the online MSDN for your further information:

LINQ to XML Classes Overview
http://msdn.microsoft.com/en-us/library/bb387023(v=VS.90).aspx

Pretty simple to use.
Seems that you did not see my first post: http://www.dreamincode.net/forums/topic/114565-read-in-xml-file-from-internet/

You did not use XDocument in your follow-up codes.
Avatar of GAM3R

ASKER

@trestan you wanna set a XDocument method up for me? I honestly don't understand the page.
Just add "using System.Xml.Linq;" in your using region at the top of your file. Then add  "XDocument x = XDocument.Load("http://cheatingzone.net/LoginSystem.xml");"  replacing where you are using "XmlDocument " .

Guess you can get a kick off from here.  
ASKER CERTIFIED SOLUTION
Avatar of AlfredRobot
AlfredRobot
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of GAM3R

ASKER

@ Alfred that code is for this XML? Or the other one a couple posts up. A

<Users count="2">
  <user username="Test1" password="testPass" joinDate="07/11/2010" />
  <user username="Test2" password="testPass" joinDate="07/11/2010" />
  </Users>

yes, this code parse

<Users count="2">
  <user username="Test1" password="testPass" joinDate="07/11/2010" />
  <user username="Test2" password="testPass" joinDate="07/11/2010" />
 </Users>

but I didn't read out the joinDate information. I got the usernames and passwords.
Avatar of GAM3R

ASKER

This code should work perfectly correct?
        private void cmdLogin_Click(object sender, EventArgs e)
        {
            XDocument x = XDocument.Load("http://cheatingzone.net/LoginSystem.xml");
            XElement xroot = x.Element("Users");
            //List<string> usernames = new List<string>();
            //List<string> passwords = new List<string>();

            XElement[] ele = xroot.Descendants().ToArray<XElement>();

            if (ele != null)
            {
                foreach (XElement new_e in ele)
                {
                    if (new_e.Attribute("username").Value == txtUsername.Text && new_e.Attribute("password").Value == txtPassword.Text)
                    {
                        expandablePanel1.Text = txtUsername.Text;
                        break;
                    }

                    //usernames.Add(new_e.Attribute("username").Value);
                    //passwords.Add(new_e.Attribute("password").Value);
                }
            }
        }

Open in new window

>>This code should work perfectly correct?

Yes.
Avatar of GAM3R

ASKER

When I went to run the method the whole program froze. What should I do?
Avatar of GAM3R

ASKER

The error I get is Name cannot begin with the '0' character, hexadecimal value 0x30. Line 3, position 52. and it points to the URL.
Avatar of GAM3R

ASKER

For Some reason it won't login, I type the right information but nothing happens.
Avatar of GAM3R

ASKER

Found out what I was doing...