1

I have some XML with an element like this:

<hour base_forecast="12" datim="29/0" />

And am receiving the error:

Unexpected node type Element. ReadElementString method can only be 
called on elements with simple or empty content.

I am guessing this is because the element has no value. I don't control this XML so I can't change it. How would I deserialize this?

** EDIT **

One of the attributes' values is ">6" .... could this be the culprit? If so, how do I handle that?

** Update **

Found some data that wasn't returning a > in a value of the attribute. Same error is occurring.

** Edit #3 * Created an XSD for the XML I am receiving, then generated classes for them with the xsd tool. Adding to the bottom of this post.

Here is the Deserialization code:

        HttpWebRequest req = (HttpWebRequest)WebRequest.Create("xxx");
        HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
        WeatherData Result = new WeatherData();

        using (Stream st = resp.GetResponseStream())
        {
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "model_data";
            xRoot.IsNullable = true;

            Result = new XmlSerializer(typeof(WeatherData), xRoot).Deserialize(st) as WeatherData;  ** Error here

Xml returned:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE observation SYSTEM "http://private.com/hithere.dtd">
<model_data>
     <site a="28/12" b="KXXX">
         <hour x="-9999" y="-9999" z="-9999"/>
     </site>
</model_data>

Data object

[Serializable, XmlRoot("model_data")]
public class WeatherData
{
    [XmlElement("site")]
    public string City { get; set; }

    [XmlAttribute]
    public string a { get; set; }

    [XmlAttribute]
    public string b { get; set; }

    [XmlElement(ElementName="hour", IsNullable=true)]
    public string Hour { get; set; }

    [XmlAttribute]
    public string x { get; set; }

    [XmlAttribute]
    public string y { get; set; }

    [XmlAttribute]
    public string z { get; set; }

}

XSD Tool generated classes

**Removed generated classes, but they are similar to what Hugo posted **
TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43
  • Is it possible that you are calling `ReadElementString` from the parent of `` or your reader is not where you think it is? – Tung Apr 28 '12 at 19:17
  • Will edit post and provide some code. – TheGeekYouNeed Apr 28 '12 at 20:53
  • If I dump the stream into an XmlDocument then try deserializing, I see that the deserializer is at element hour when it throws the error. In this format, the error is throw at the correct line and position that begins as well. – TheGeekYouNeed Apr 28 '12 at 21:08
  • What exactly are you trying to get into `[XmlElement("site")]public string City { get; set; }` – Ilia G Apr 28 '12 at 21:43

2 Answers2

0

Looking at this part:

[XmlElement("site")]
public string City { get; set; }

<Site> contains <Hour>, so it is not an element with simple or empty content, I guess?

Edit: actually the whole thing seems suspect. The data object seems to disregard all hierarchy information in the xml.

How about something like this?

[Serializable, XmlRoot("model_data")]
public class WeatherData
{
    [XmlElement("site")]
    public City[] City { get; set; }
}

public class City
{
    [XmlAttribute]
    public string a { get; set; }

    [XmlAttribute]
    public string b { get; set; }

    [XmlElement(ElementName="hour", IsNullable=true)]
    public Hour Hour { get; set; }
}

public class Hour 
{    
    [XmlAttribute]
    public string x { get; set; }

    [XmlAttribute]
    public string y { get; set; }

    [XmlAttribute]
    public string z { get; set; }   
}
HugoRune
  • 13,157
  • 7
  • 69
  • 144
  • I changed my object class to match the xml element names exactly but am still receiving the same error. – TheGeekYouNeed Apr 28 '12 at 21:52
  • @TheGeekYouNeed: it is not the name discrepancy that worries me; you are trying to put an element with child elements into a string, what is that string supposed to contain? – HugoRune Apr 28 '12 at 22:00
  • I created an XSD from the xml, then generated classes from that XSD, and am still receiving the same error. – TheGeekYouNeed Apr 28 '12 at 22:20
  • @TheGeekYouNeed: even in the data without the unescaped ">6"? That is weird. What do the generated classes look like? You could also try generating an xsd from your dtd: http://stackoverflow.com/questions/1510126/free-dtd-to-xsd-conversion-utility – HugoRune Apr 28 '12 at 22:26
  • Yes, even in the data without the >6. I'll update the post with the generated classes. – TheGeekYouNeed Apr 28 '12 at 22:30
  • Well, in that case I am out of ideas for now. You could check whether the xml is malformed with one of the many xml validators, but I guess if it passed the xsd converter, it should be ok. – HugoRune Apr 28 '12 at 22:42
  • Thank you for your time invested! You were right, my classes were wrong. – TheGeekYouNeed Apr 28 '12 at 23:10
0

When I generated the classes with the XSD tool, I getting the same error, but the error was showing being thrown from a line I had commented out.

So I exited VS, and ran disk cleanup. Ran my code again. Received a message "For security reasons DTD is prohibited in this XML document. etc." So, I allowed set the reader to DtdProcessing.Parse, and ran the code once more.

It was successful.

TheGeekYouNeed
  • 7,509
  • 2
  • 26
  • 43