Jesse
Jesse

Reputation: 1

How to read XML text (not attributes) into float?

Hey guys I am trying to read 3D points (X, Y, Z) (all Float) from an XML file in C#.

The XML for each point used to be formatted like this:

<Point X="-4865.764" Y="-4945.29" Z="261.1602"/>

and I could read this with the following:

return new XElement("Point", new XAttribute("X", X), new XAttribute("Y", Y), new XAttribute("Z", Z));

But now I have to read my points from XML formatted like this:

<Point>679.7905 -4312.875 60.93259</Point> 

How can I read the XML into my float variables (X, Y and Z) when it is formatted as shown above?

Thanks alot,

Jesse

Upvotes: 0

Views: 2385

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502816

You'll need to split the value, e.g.

string[] values = element.Value.Split(' ');
// Possibly do validation here to check there are 3 values?
// Note the specification of the culture here - otherwise if you're in a culture
// which uses "," as the decimal separator, it won't do what you want...
float x = float.Parse(values[0], CultureInfo.InvariantCulture);
float y = float.Parse(values[1], CultureInfo.InvariantCulture);
float z = float.Parse(values[2], CultureInfo.InvariantCulture);

Upvotes: 2

Related Questions