Reading Online XML File

GAM3R
GAM3R used Ask the Experts™
on
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

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
When I use my URL http://cheatingzone.net/LoginSystem.xml I get the error Unable to connect to the remote server
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

just tried, this works in my computer:
XDocument x = XDocument.Load("http://cheatingzone.net/LoginSystem.xml");
not sure why it does work to you.

Author

Commented:

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

Author

Commented:
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");

Commented:
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.

Commented:
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.

Author

Commented:
@trestan you wanna set a XDocument method up for me? I honestly don't understand the page.

Commented:
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.  
using this piece of codes:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace xmlreader
{
    class Program
    {
        static void Main(string[] args)
        {
            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 e in ele)
                {
                    usernames.Add(e.Attribute("username").Value);
                    passwords.Add(e.Attribute("password").Value);
                }
            }
            // do whatever you want
            // data are saved in these two lists
        }
    }
}

Author

Commented:
@ 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.

Author

Commented:
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

Commented:
>>This code should work perfectly correct?

Yes.

Author

Commented:
When I went to run the method the whole program froze. What should I do?

Author

Commented:
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.

Author

Commented:
For Some reason it won't login, I type the right information but nothing happens.

Author

Commented:
Found out what I was doing...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial