0

Is there a way to read XML file into a list but not create a list inside of a list when only having one element.

<DewesoftSetup>
   <Devices>
        <StartStoreTime>43874.6969328704</StartStoreTime>
        <SampleRate>50000</SampleRate>
        <BlockSize>1000</BlockSize>
        <Device Type="AI">
            <Slot Index="0">
                <MeasuredValue>VOLTAGE</MeasuredValue>
                <Range>10 V</Range>
                <LPFilter_Type>BU</LPFilter_Type>
                <LPFilter_Hz>10000</LPFilter_Hz>
                <LPFilter_Order>2</LPFilter_Order>
                <AmplifierChainInfo>
                    <AmplifierInfo Index="0" Type="DEWEUSB_AMPLIFIER">
                        <Name>SIRIUS-HD-ACC</Name>
                        <SerialNr>D013C47522</SerialNr>
                        <Revision>1.3.0.0</Revision>
                        <Firmware>1.10</Firmware>
                        <CalDate>09.01.2020</CalDate>
                        <Measurement>Voltage</Measurement>
                        <Range>10 V</Range>
                        <LP_filter>10 kHz*  (HW: 100 kHz)</LP_filter>
                        <LP_filter_type>Butterworth*</LP_filter_type>
                        <LP_filter_order>2nd*</LP_filter_order>
                        <HP_filter>AC  1 Hz  (SW: 0.1 Hz)</HP_filter>
                    </AmplifierInfo>
                </AmplifierChainInfo>
            </Slot>
   </Devices>  
</DewesoftSetup>     

I only have one Amplifier per slot. I am making a list based on the slots but the only way I know how to get the data from ````` is to create another list.

 var devices = from y in xdoc.Descendants("DewesoftSetup").Descendants("Device").Where(e => e.Attribute("Type").Value == "AI").Descendants("Slot")
                       select new DewesoftDevices
                       {
                          slotNum = y.Attribute("Index").Value,
                          measuredValue = y.Element("MeasuredValue").Value,
                          range = y.Element("Range").Value,
                          lpFilterType = y.Element("LPFilter_Type").Value,
                          lpFilter = y.Element("LPFilter_Hz").Value,
                          lpFilterOrder = y.Element("LPFilter_Order").Value,
                          dewesoftAmplifier = new DewesoftAmplifier()
                          {
                             ampInfo = new List<AmplifierInfo>(from ampOutput in y.Descendants("AmplifierChainInfo").Descendants("AmplifierInfo")
                                                               select new AmplifierInfo
                                                               {
                                                                  type = ampOutput.Attribute("Type").Value,
                                                                  name = ampOutput.Element("Name").Value,
                                                                  cardSerialNumber = ampOutput.Element("SerialNr").Value,
                                                                  revision = ampOutput.Element("Revision").Value,
                                                                  calDate = ampOutput.Element("CalDate").Value,
                                                                  range = ampOutput.Element("Range").Value,
                                                                  lpFilter = ampOutput.Element("LP_filter").Value,
                                                                  lpFilterType = ampOutput.Element("LP_filter_type").Value,
                                                                  lpFilterOrder = ampOutput.Element("LP_filter_order").Value,
                                                                  hpFilter = ampOutput.Element("HP_filter").Value
                                                               })
                          }

Is there a way to just create one instance in each slot instead of creating another list int each slot?

  • I don't know the answer using LINQ, but I don't think this is the best approach. I would setup some classes to represent the objects and deseralise the XML that way, as opposed to using LINQ. This question does it that way. https://stackoverflow.com/questions/10518372/how-to-deserialize-xml-to-object Happy to provide a specific example if required. – Ryan Thomas Feb 14 '20 at 14:31
  • After getting the devices variable, you can check ampInfo.count and if it is 1, you can store it as one instance – Curious Feb 14 '20 at 14:35
  • @Curious How do you just store it as one instance? Its always one for each slot. –  Feb 14 '20 at 14:38
  • @RyanThomas I will look into that! –  Feb 14 '20 at 14:38
  • @RyanThomas How would I go about the class structure for deserialization if there are different device types and i need to index the slots in a list? –  Feb 14 '20 at 15:03
  • You can use FirstOrDefault() to flatten the array or use SelectMany instead of Select. – jdweng Feb 14 '20 at 15:49

0 Answers0