0

I have this xml as you can see :

<ResultTest>
  <BackLeftShockAbsorber>0</BackLeftShockAbsorber>
  <BackRightBrak>0</BackRightBrak>
  <BackRightShockAbsorber>0</BackRightShockAbsorber>
  <BackShockAbsorber>0</BackShockAbsorber>
  <BackSideSlip>0</BackSideSlip>
  <BackWeight>0</BackWeight>
  <BrakeAcceleration>0</BrakeAcceleration>
  <CarReceiptionId>7</CarReceiptionId>
  <CO>0</CO>
  <CO2>0</CO2>
  <FrontBrake>0</FrontBrake>
  <FrontLeftBrake>0</FrontLeftBrake>
  <FrontLeftShockAbsorber>0</FrontLeftShockAbsorber>
  <FrontRightShockAbsorber>0</FrontRightShockAbsorber>
  <FrontShockAbsorber>0</FrontShockAbsorber>
  <FrontSideSlip>0</FrontSideSlip>
  <FrontWeight>0</FrontWeight>
  <FuelSystem>0</FuelSystem>
  <HandBrake>0</HandBrake>
  <HandBrake>0</HandBrake>
  <HandBrakeAcceleration>0</HandBrakeAcceleration>
  <HandBrakeLeft>0</HandBrakeLeft>
  <HandBrakeRight>0</HandBrakeRight>
  <LabelApplication>0</LabelApplication>
  <Lambda>0</Lambda>
  <NOX>0</NOX>
  <O2>0</O2>
  <Opacity>0</Opacity>
  <OutsideState>0</OutsideState>
  <PlayDetectorState>0</PlayDetectorState>
  <TechnicalReviewCenterLineId>0</TechnicalReviewCenterLineId>
  <TotalWeight>0</TotalWeight>
</ResultTest>

I want to get the data in each element as you can see here :

        float CurrentData = float.Parse(xd.Root.Elements("CO").ToString());

But i get this error :

An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code

Additional information: Input string was not in a correct format.
Ehsan Akbar
  • 6,977
  • 19
  • 96
  • 180
  • I think exception message is pretty clear. Value in `CO` element cannot be parsed to `float` type. You didn't show actual values, because `float.parse("0")` will be successfully parsed. – Fabio Jul 08 '17 at 07:32

1 Answers1

1

Real simple to take this xml and put into a dictionary

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            Dictionary<string, int> dict = doc.Descendants("ResultTest").Elements()
                .GroupBy(x => x.Name.LocalName, y => (int)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • How `Dictionary` will help with OP's parsing exception? – Fabio Jul 08 '17 at 07:33
  • My code doesn't get the exception because I'm using a totally different method that required at least 1/30 of the amount of code the user will need. – jdweng Jul 08 '17 at 07:45
  • Your code will throw exception in case when value of `XElement` is not correctly formatted integer. And OP try to get `float` type. – Fabio Jul 08 '17 at 07:50
  • In addition you don't need `GroupBy` at all, because all elements are unique in the OP's example. – Fabio Jul 08 '17 at 07:53
  • The code works. None of the values appear to be float so why use float. The GroupBy makes the code simpler since I'm getting the key and I'm converting the element to an integer in the GroupBy. – jdweng Jul 08 '17 at 07:59
  • You can do it in `ToDictionary` which makes code little bid simpler `.ToDictionary(element => element.Name.LocalName, element => (int)element);` – Fabio Jul 08 '17 at 08:01
  • OP clearly show the line where he try to parse string to `float` type and obviously OP doesn't show actual values in xml, because "0" will parse to `float`. – Fabio Jul 08 '17 at 08:03
  • The dictionary is much simpler than other methods. If the user needs floats for all number it is very simply to change the int in two places to float. – jdweng Jul 08 '17 at 08:05
  • Casting element to int will not a solution for this case, because explicit operator XElement to int will use same `int.Parse` under the hood – Fabio Jul 08 '17 at 08:06
  • @jdweng in this case all data is int but it is possible to be float . – Ehsan Akbar Jul 08 '17 at 08:08
  • In any cases better approach will be create a class `ResultTest` with properties with correct types and use `XmlSerializer` for deserialization. You will get intellisense help when using deserialized object, deserialization will happenned in one line, and all values will be in correct type. – Fabio Jul 08 '17 at 08:09
  • Using serializer in this case is ridiculous. It would add a lot of extra code to reference each object by property name. – jdweng Jul 08 '17 at 08:12
  • With dictionary you will end up with typing/hardcoding names of elements all around your application. – Fabio Jul 08 '17 at 08:21
  • When elements have different types dictionary become useless. – Fabio Jul 08 '17 at 08:22
  • Fabio : You are obvious a novice programmer. – jdweng Jul 08 '17 at 09:25