Jump to content

Reading from externally stored txt or xml file

- - - - -

  • Please log in to reply
3 replies to this topic

#1
sonar87

sonar87

    Newbie

  • Members
  • PipPip
  • 15 posts
For a program I'm working on I want to be able to read from text or xml file (it doesn't really mater which) that stored some place else, like a file hosting site of some sort. The idea being that could go change something in the file if needed, then all the copies of the program, if I'd give to multiple people, would then have the changes. That or whatever else I could think to do with it, just pkaying around trying to get something basic set up. I've tryied reading the file with both the normal method of reading files in C# with a streamreader, and making it an xml file and using the xmlreader. Neither one will take the uri that can be used to access the hosted file.

So is there any way I can get it access the file stored like this? What other option would I have for doing something like this if not?

#2
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
I guess one option would be to stick the file on a http server, or an ftp server. The framework has lots of options built right in to support something like this.
Let me show you a really straight forward way, of say, reading XML from an Http site.


//make sure you import the System.Xml namespace in your source file.


string read_xml_file_value() {

    XmlDocument xdoc = new XmlDocument();

    xdoc.Load("http://www.mysite.com/files/file.xml");

    //do your regular normal processing, eg

    return xdoc.SelectSingleNode(string.format(@"/people/person[@name_first='{0}']", "sam")).InnerText;

}



well, you get the idea. That's one way. Another way might be to download the contents of the file first, and then load it.
you could do this with a webrequest or possibly a socket... I'm not using my IDE, this should be considered just pseudo code, just doing it from memory, but if you want me to elaborate, I'll type reply with something that will for sure compile...



using (WebRequest request = WebRequest.Create("http://www.mysite.com/files/file.xml")) {

     using (WebResponse response = request.GetResponse()) {

          using (StreamReader reader = new StreamReader(response.ResponseStream)) {

              //from here, you could use the reader, to extract the contents of the stream

              string contents = reader.ReadToEnd(); //or whatever

              xdoc.LoadXml(contents);

          }

     }

}



anyways, hope that gets your thinker thinking. Let me know if im hitting the mark, and if you want some actual code to do that, or something different. I'm sure we could figure it out.

#3
sonar87

sonar87

    Newbie

  • Members
  • PipPip
  • 15 posts
Sorry for such a late replay, we've been having problems with internet. Following your first example I've got it working. I'm wishing I'd kept the code from my attempts now though, because I know in one attempt I did it almost exactly like that. Would be nice to have seen exactly what I was getting wrong. :/

Anyway, thanks for the help. For the second example as well, as its brought my attention to something new to me.

#4
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
no problem-o! glad to have helped!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users