开发者

Is there a better way to get XmlReader NodeType properties?

Based on this, it looks like each time I want to get a node element nam开发者_JS百科e, or a text value or whatever, I need to use switch-case.

So do I have to use something like this:

while (reader.Read())
{
    switch (reader.NodeType)
    {
        case XmlNodeType.Element:
        nodeName = reader.Name;
        break;
    }
}

or am I missing something and it could be made shorter and more elegant (I need to use XmlReader, and am not in position to use LINQ to XML)? The idea of having to use all this switch-case mess everywhere just to get these small bits of information is not very appealing to me. Thank you.


There is no casting beeing done, so as far as i can see you can just go:

while(reader.Read())
{
    nodeName = reader.Name;
}

I believes nodes always have a name atleast, i might be wrong though, but afaik they do.


The one thing about the Switch case is that it's very clear about your intentions, which I believe is an important thing in modern programming.

However, if you're tired of writing it all the time, and I can't blame you, what about wrapping it in an Extension method ?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜