0

I dont really understand the usage of "XmlReader.MoveToNextAttribute()". Because when i read about it in microsoft docs, it only return value true or false. And i don't understand why we should use "while loop" on it in this code :

if (reader.HasAttributes) {
    Console.WriteLine("Attributes of <" + reader.Name + ">");
    while (reader.MoveToNextAttribute()) {
          Console.WriteLine(" {0}={1}", reader.Name, reader.Value);
    }
}

Can anyone properly explain to me what is the real usage of "XmlReader.MoveToNextAttribute()" ?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • 3
    Have you ever used, e.g., `IEnumerable.GetEnumerator()`? Then you could `MoveNext()`, which of course returns a `bool` if the method succeeded, so you know it moved Current to the next element in the collection. If it returns `false`, you can go no further. So you can use something like `while (reader.MoveToNextAttribute()) { }` to read the Current value until the loop goes on. Or `while ([IDataReader].Read()) { }` etc. – Jimi Mar 16 '21 at 16:33
  • Oh i just know it, thanks for telling me. – Verlander store Mar 16 '21 at 16:40
  • After the loop you may want to use [`XmlReader.MoveToElement()`](https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreader.movetoelement?view=net-5.0) to move back to the element itself. – dbc Mar 17 '21 at 13:26
  • Does [How to make an XmlReader read the attributes in C#?](https://stackoverflow.com/a/37864858/3744182) answer your question? – dbc Mar 17 '21 at 13:45

0 Answers0