2011-01-27 73 views
0

嘿家伙我试图从C#中的XML文件中读取3D点(X,Y,Z)(所有浮点数)。如何将XML文本(不是属性)读入浮点数?

使用的每个点上的XML进行格式化这样的:

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

,我可以用下面的阅读:

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

但现在我必须从XML格式的读取我的观点像这样:

<Point>679.7905 -4312.875 60.93259</Point> 

我怎样才能读取XML到我的浮点型变量(X,Y和Z)时,它是格式如上所示?

非常感谢,

杰西

回答

2

你需要拆分值,例如

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);