V
V
Vladimir Korotenko2020-06-15 17:02:53
C++ / C#
Vladimir Korotenko, 2020-06-15 17:02:53

XmlReader skips tags, how to overcome?

https://docs.microsoft.com/ru-ru/dotnet/api/system...

An extremely interesting situation. XmlReader skips tags and every other time.

while (_reader.Read())
            {
                switch (_reader.NodeType)
                {
                    case XmlNodeType.Element:
                        if (_reader.Name == Tags.CompanyTag)
                        {
                            var c = new Company(_reader.ReadOuterXml());
                            PutInDictionary(c);
                            OnCompanyParsed(this, c);
                        }
                        break;
                }
            }


And he does it in the meanest way.
Handles correctly

<?xml version="1.0" encoding="utf-8" ?>
<companies_list>
  <company>
    <name>A</name>
  </company>
  <company>
    <name>B</name>
  </company>
  <company>
    <name>C</name>
  </company>
</companies_list>


Incorrect, selects only 1 and 3 elements.
<?xml version="1.0" encoding="utf-8" ?>
<companies_list><company><name>A</name></company><company><name>B</name></company><company><name>C</name></company></companies_list>


How to deal with it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2020-06-15
@firedragon

Because you read twice in a row.
_reader.ReadOuterXml()=> Here you are already on B
A then
while (_reader.Read())=> Go toC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question