'Errors de-serializing XML: {"<query xmlns=''> was not expected."}
UPDATE: I've solved the problem. The model called class symbols has been updated and now works to deserialize the XML correctly. Whew! My object now deserializes correctly.
I normally work with json, but have to work with XML to use this data.
I've tried this several different ways but am unable to get this to deserialize.The typical error it throws is:
{"<query xmlns=''> was not expected."}
I think it's how I've set up the Query class. I've tried several different things with no luck. Hoping someone that knows more about XML can offer a suggestion. Thanks.
Here is the GetResult method that I call from a console app.
public static Query GetResults()
{
string url = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.industry%20where%20id%20in%20(select%20industry.id%20from%20yahoo.finance.sectors)&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
Query resultSet;
using (WebResponse response = request.GetResponse()) {
using (Stream responseStream = response.GetResponseStream()) {
XmlSerializer serializer = new XmlSerializer(typeof(Results));
resultSet = (Query)serializer.Deserialize(responseStream);
}
}
return resultSet;
}
Here is the Results class: NOTE: This class has been modified to work correctly with the XML. This now works!
namespace TheXMLService
{
[XmlRoot("query", Namespace = "")]
public class Symbols
{
[XmlArrayItem("industry", typeof(Industry))]
public List<Industry> results { get; set; }
}
public class Industry
{
[XmlAttribute()]
public int id { get; set; }
[XmlAttribute()]
public string name { get; set; }
[XmlElement("company", typeof(Company))]
public Company[] company;
}
public class Company
{
[XmlAttribute()]
public string name;
[XmlAttribute()]
public string symbol;
}
}
And here is a chunk of the xml that gets returned:
<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="215" yahoo:created="2014-02-12T18:40:10Z" yahoo:lang="en-US">
<results>
<industry id="112" name="Agricultural Chemicals">
<company name="Adarsh Plant Protect Ltd." symbol="ADARSHPL.BO"/>
<company name="AgriTec Systems, Inc." symbol="AGR.V"/>
<company name="Agrium Inc" symbol="AGU.DE"/>
... (many more companies here...
<company name="United Utilities Group PLC" symbol="UUEC.DE"/>
</industry>
</results>
</query>
<!-- total: 3288 -->
<!-- engine2.yql.bf1.yahoo.com -->
Solution 1:[1]
It seems, you should change
XmlSerializer serializer = new XmlSerializer(typeof(Results));
to
XmlSerializer serializer = new XmlSerializer(typeof(Query));
and
[XmlRoot("query", Namespace = "http://www.yahooapis.com/v1/base.rng")]
to
[XmlRoot("query")]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | L.B |
