0

so I've created a simple RESTful service with netbeans and produce an XML file in localhost http://localhost:8080/testXML/webresources/entities.categoryid

inside this page is only a simple xml file format:

<categoryids>
  <categoryid>
    <CategoryID>id1111</CategoryID>
    <CategoryName>Study</CategoryName>
  </categoryid>
</categoryids>

I'm using C# to read the xml files, put them into dataset and finally datagridview. how do I do this? it works locally (sample.xml) with

ds.ReadXml(Application.StartupPath + "\\XML\\sample.xml", XmlReadMode.Auto);
dv = ds.Tables[0].DefaultView; 

however it doesn't work with localhost

ds.ReadXml("http://localhost:8080/testXML/webresources/entities.categoryid", XmlReadMode.Auto);

Are there any way to do this?

edit:

NullRferenceException: Object reference not set to an instance of an object.

indicating that the datagridview is null

edit2: sorry, it's supposed to be "ReadXml" not "WriteXml"

Asmo
  • 231
  • 1
  • 3
  • 8
  • 1
    Please don't ever tell us "it doesn't work". Tell us, specifically, _how_ it doesn't work. Is there an exception? Post the full exception! – John Saunders Oct 17 '13 at 06:45
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 17 '13 at 06:48
  • Where are you getting the exception? I'd expect the `WriteXml` call to fail before the `ReadXml` call in the first place... – Jon Skeet Oct 17 '13 at 06:49
  • @JonSkeet sorry for the misunderstanding. It's ReadXml, not WriteXml – Asmo Oct 17 '13 at 06:53
  • That's not "misunderstanding" - that's "writing a completely bogus question". You're now calling `ReadXml` twice - why? – Jon Skeet Oct 17 '13 at 06:55
  • @JonSkeet oh my god. bogus question indeed. edited. – Asmo Oct 17 '13 at 06:56
  • And which line is giving you the exception? Is `ds` null, or `dv`? Or something else? – Jon Skeet Oct 17 '13 at 06:59
  • @JonSkeet The datagridview shows nothing. however, when it is clicked, it gives that exception. Generally the exception is from private void dataGridView1_Click with string parse method in it. hopefully this screenshot will help understanding the problem: http://imgur.com/znFIvVc – Asmo Oct 17 '13 at 07:11

1 Answers1

2

The problem is that your DataSet.ReadXml()-Method can not read from a URL, because you have to send a GET-Request (referring to http://msdn.microsoft.com/en-us/library/system.data.dataset.readxml%28v=vs.80%29.aspx). You need to send a Webrequest to speak with your RESTful-Service:

WebRequest request = WebRequest.Create ("http://localhost:8080/testXML/webresources/entities.categoryid");
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
Stream dataStream = response.GetResponseStream ();
ds.ReadXml(dataStream);
dataStream.Close();
response.Close();
...

I hope that will work for you.

Taemyr
  • 3,407
  • 16
  • 26
Seb Krae
  • 311
  • 3
  • 10