1

I am trying to make a web service that returns the list of response from another domain but I am having trouble converting the WebResponse to DataTable so I can return it as a list. So basically the flow is WebResponse -> DataTable -> List so far I have this

public string getAgentStat()
    {

        WebRequest request = WebRequest.Create("http://1.1.1.1/test/testweb.dll?Tenant=test&Filter=Calls");
        request.Credentials = new NetworkCredential("test@test.com", "P@ssw0rd123");
        request.Method = "GET";
        WebResponse response = request.GetResponse();
        DataTable table = new DataTable();


        System.IO.StreamReader stream = new System.IO.StreamReader(response.GetResponseStream());
        table.ReadXml(stream);

        return stream.ReadToEnd();
    }

but I am encountering an error on the code line table.ReadXml(stream) the error says

DataTable does not support schema inference from Xml

As much as possible I like to do it as fast so avoiding looping in converting the webresponse to Datatable is a must.

Any suggestion is accepted. If you think my way is inefficient.

Edit 1: The list that will be returned

public class test
{
  public int cw;
}

static List<test> test_ = new List<test> {};

Edit 2: The XML returned

<QCalls ts="Fri Jan 11 01:30:27 2019 UTC" cts="Fri Jan 11 01:27:03 2019 UTC" tzo="-28800" al="false">
<Q id="1" n="testLo" wt="151516" ch="29" cwt="1635" ct="25" co="29" ca="0" cw="4" awt="56904" act="282" cbh="3" ofi="0" ofo="0" catqos="0" dlro="275">
<internet id="1" n="RetailerLo"/>
<message id="1" n="RetailerLo"/>
<phone id="1" n="RetailerLo" wt="0" ch="22" cwt="23" ct="22" co="22" ca="0" cw="0" awt="0" act="372" cbh="3" ofi="0" ofo="0" catqos="0"/>
<callback id="1" n="RetailerLo" wt="151516" ch="7" cwt="1612" ct="3" co="7" cw="4" awt="56904" act="0" cbh="0"/>
<O id="0" cnt="1"/>
</Q>
<Q id="1" n="testHi" wt="36326" ch="9" cwt="41790" ct="7" co="7" ca="0" cw="1" awt="36326" act="770" cbh="2" ofo="0" catqos="0" dlro="26">
<internet id="1" n="RetailerHi"/>
<message id="1" n="RetailerHi"/>
<phone id="1" n="RetailerHi" wt="0" ch="8" cwt="7" ct="7" co="7" ca="0" cw="0" awt="0" act="866" cbh="2" ofo="0" catqos="0"/>
<callback id="1" n="RetailerHi" wt="36326" ch="1" cwt="41783" ct="0" co="0" cw="1" awt="36326" act="1" cbh="0"/>
<O id="0" cnt="26"/>
</Q>
</QCalls>
Vian Ojeda Garcia
  • 827
  • 4
  • 17
  • 34
  • _"As much as possible I like to do it as fast so avoiding looping is a must."_ - you want a list, there will be looping involved. – ProgrammingLlama Jan 11 '19 at 02:04
  • What I mean on this statement is when converting webresponse to dataTable – Vian Ojeda Garcia Jan 11 '19 at 02:06
  • Why do you want to convert it to a DataTable if you want a list? In one part it sounds like you want efficiency, but then your approach smells of inefficiency. Why not just deserialize your XML to a list? What is it a list of, anyway? – ProgrammingLlama Jan 11 '19 at 02:07
  • Thank you for that feedback @John I am not familiar with this territory so any suggestion is accepted sir. I am planning to return this as a list – Vian Ojeda Garcia Jan 11 '19 at 02:10
  • But what is it a list of? That's kind of important. – ProgrammingLlama Jan 11 '19 at 02:14
  • @John on my practice which I am not sure practical since it's in question. When I am creating a method in a web service. I always return it as a list sir. – Vian Ojeda Garcia Jan 11 '19 at 02:16
  • OK. I'm out. Good luck. Without trying to fix your method, I Googled and found [this question](https://stackoverflow.com/questions/1935522/datatable-does-not-support-schema-inference-from-xml) which might help you. – ProgrammingLlama Jan 11 '19 at 02:17
  • @John thank you sir I will check it out. Sorry if its not a problem to you, can you explain what is wrong with the way I am doing this? – Vian Ojeda Garcia Jan 11 '19 at 02:21
  • Well, it's difficult since you won't tell us what the list contains. For all I know it's `List`, `List`, `List`, `List`, etc. But normally, you would deserialize the XML to a list (like [this](https://stackoverflow.com/questions/608110/is-it-possible-to-deserialize-xml-into-listt)) rather than reading the XML into a DataTable and then converting that to a list somehow (again you don't tell us what the list contains). – ProgrammingLlama Jan 11 '19 at 02:25
  • @John oh that's what you mean. I will edit the post so maybe you can help me? ill go check out the link you sent too. – Vian Ojeda Garcia Jan 11 '19 at 02:27
  • Yes, if you can edit the post to clarify all the details (if you're deserializing to something like `List` then please include the definition of that class), I might be able to help :) – ProgrammingLlama Jan 11 '19 at 02:28
  • Can you also include your XML, please? – ProgrammingLlama Jan 11 '19 at 02:31
  • @john I already added some info on the post but its just basic stuff. But basically I need that cw data from webresponse so I can return it on my web service. – Vian Ojeda Garcia Jan 11 '19 at 02:31
  • @john I just need the cw data on the Q tag – Vian Ojeda Garcia Jan 11 '19 at 02:35

1 Answers1

2

You could achieve this by using the XmlSerializer:

[XmlRoot("QCalls")]
public class QCalls
{
    [XmlElement("Q")]
    public List<QItem> Items { get; set; }
}

public class QItem
{
    [XmlAttribute("cw")]
    public int CW { get; set; }
}

// Deserialization code
QCalls calls = null;
var serializer = new XmlSerializer(typeof(QCalls));
calls = (QCalls)serializer.Deserialize(response.GetResponseStream());
return calls.Items;

Try it online

Note that it's possible you might get an exception on the var serializer = line. This is an internal thing and is handled internally by the XmlSerializer. For more info see the question here.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86