Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Reading from externally stored txt or xml file

xmlreader xml

  • Please log in to reply
3 replies to this topic

#1 sonar87

sonar87

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 09 March 2011 - 07:10 PM

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?
  • 0

#2 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 09 March 2011 - 07:34 PM

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.
  • 1

#3 sonar87

sonar87

    CC Newcomer

  • Member
  • PipPip
  • 21 posts

Posted 14 March 2011 - 09:53 PM

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.
  • 0

#4 sam_coder

sam_coder

    CC Addict

  • Senior Member
  • PipPipPipPipPip
  • 380 posts

Posted 15 March 2011 - 07:41 PM

no problem-o! glad to have helped!
  • 0





Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download