开发者

XmlNodeReader returns {None}

I'm currently having trouble deserializing an XmlDocument from a web service call, here is my code : -


 public void getTest(XmlDocument requestDoc)
    {
        XmlDocument results = new XmlDocument();
        XmlSerializer serial = new XmlSerializer(typeof(DataRequest));
        DataRequest req;
        XmlNodeReader reader = new XmlNodeReader(requestDoc.DocumentElement);
        req = (DataRequest)serial.Deserialize(reader);
        response.write(req.toString());
    }

now, the trouble I am having is that the XmlNodeReader just contains "{None}" when I step through in debug, the requestDoc definatly has the expected XM开发者_开发技巧L structure, any ideas?

Kind regards Gib


The "none" probably just means it hasn't started iterating yet, and is at BOF (for want of a better term). It should still work. Usually, if it doesn't it means the namespaces are incorrect - double-check for xmlns in the source.

This works fine, for example:

public class Test
{
    static void Main()
    {
        var doc = new XmlDocument();
        doc.LoadXml(@"<Test foo=""bar""></Test>");
        var ser = new XmlSerializer(typeof(Test));
        using (var reader = new XmlNodeReader(doc.DocumentElement))
        {
            var test = (Test)ser.Deserialize(reader);
            Console.WriteLine(test.Foo);
        }

    }
    [XmlAttribute("foo")]
    public string Foo { get; set; }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜