Search code examples
c#xmlxmlreader

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 name, 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.


Solution

  • 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.