0

I have this type of XML format:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <descriptor>
    <feature>5.12</feature>
    <feature>0.0002827647</feature>
    <feature>0.0147277</feature>
    <feature>0.00037847</feature>
 </descriptor>
</root>

using the MSDN example I'm trying to read it like this:

Matrix<float> ObjectDescriptors = new Matrix<float>(200, 4);
        XmlTextReader reader = new XmlTextReader("descriptors.xml");
        int i = -1;
        int ii = 0;
        while (reader.Read())
        {
            if (reader.Name == "feature" && ii < 4)
            {
                String currStr = reader.Value;
                ObjectDescriptors[i, ii] = Convert.ToSingle(currStr);                    
                ii++;
                if (ii == 4) ii = 0;
            }
            else if (reader.Name == "descriptor") i++;
        }

I get the following error for the line

AgrObjectDescriptors[i, ii] = Convert.ToSingle(currStr);

An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll Additional information: Input string was not in a correct format.

Seems like the node "feature" is being detected (reader = {Element, Name="feature"}) but its content is empty ("")

using System.Xml; is included

Would be nice if anyone tells me why this error occurs! Thank you!

2
  • replacedbyObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat(); solved the problem Commented Jun 30, 2015 at 3:17
  • your reader.Value is empty ..cast it to a float Commented Jun 30, 2015 at 3:18

2 Answers 2

1

reader.Read will go through each part of the XML, and the elements are not the same as the text that is inside them.

So once you find the element you are looking for, you must read the text inside.

Try replacing:

String currStr = reader.Value;

With:

String currStr = reader.ReadString();
Sign up to request clarification or add additional context in comments.

Comments

0

replaced by

ObjectDescriptors[i, ii] = reader.ReadElementContentAsFloat(); 

solved the problem. question closed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.