Why is my XmlReader.GetAttribute () not returning a value?

I am trying to parse my XML in C #.

Here is the part of the corresponding file:

<holder name="wnd_login" width="300" x="20" height="180">...</holder>

Here is the code that should read it:

while (reader.Read())
{
    if (reader.IsStartElement())
    {
        switch (reader.Name)
        {
            case "holder":
                Holder holder = new Holder(reader.GetAttribute("name"));
                ...
        }
    }
}

I read that a common mistake was to forget the check in order to see if the element was initial. I added it, but GetAttribute still returns null. Any idea?

+3
source share
1 answer

Perhaps you need to get XmlNodes first using XPath notation, and then iterate over XmlNodes like this:

foreach (XmlNode node in XmlNodes) {
     if (node ​​["holder"]. HasAttribues! = null && node ["holder"]. Attributes.Count> 1) {
        for (int i = 0; i < node["holder"].Attributes.Count; i++){
             try{
                XmlAttribute attr = node["holder"].Attributes[i];
                if (attr != null){
                     ....
                }
             }catch(XmlException xmlEx){
                // Do something here with this...output to log?
             }
        }
     }
}

, , , .

-4

Source: https://habr.com/ru/post/1734721/


All Articles