I wrote a C ++ application that generates an XML file from a class. Now I want to read the generated file again and save all the attributes and values back to the C ++ classes.
My XML writer (writes with success):
void TDescription::WriteXml( XmlWriter^ writer ) { writer->WriteStartElement( "Description" ); writer->WriteAttributeString( "Version", m_sVersion ); writer->WriteAttributeString( "Author", m_sAuthor ); writer->WriteString( m_sDescription ); writer->WriteEndElement(); }
My XML reader (throws an exception):
void TDescription::ReadXml( XmlReader^ reader ) { reader->ReadStartElement( "Description" ); m_sVersion = reader->GetAttribute( "Version" ); m_sAuthor = reader->GetAttribute( "Author" ); m_sDescription = reader->ReadString(); reader->ReadEndElement(); }
My generated xml file:
<?xml version="1.0" encoding="utf-8"?> <root Name="database" Purpose="try" Project="test"> <Description Version="1.1B" Author="it">primary</Description> </root>
Here is the exception thrown by the reader:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Xml.dll Additional information: There is an error in XML document (2, 2).
What is the problem with the code? I think the XmlReader methods were not used correctly !?
Due to answer 1, I changed the code:
reader->ReadStartElement( "root" ); reader->ReadStartElement( "Description" ); m_sVersion = reader->GetAttribute( "Version" ); m_sAuthor = reader->GetAttribute( "Author" ); m_sDescription = reader->ReadString(); reader->ReadEndElement(); reader->ReadEndElement();
Now I am not getting an exception, and m_sDescription getting the correct value, but m_sVersion and m_sAuthor are still empty.
Peter source share