Как сделать xmlroot специфическим c атрибутом в C# - PullRequest
0 голосов
/ 14 февраля 2020

Недавно я изменил свою программу для десериализации файлов XML. Я создал класс, но он должен быть под определенным атрибутом. Я думал, что использование XmlRootAttribute будет работать, но я продолжаю получать сообщение об ошибке

InvalidOperationException: <DataFileSetup xmlns=''> was not expected.

Вот мой код

[XmlRoot(ElementName = "DewesoftSetup")]
public class DewesoftDevices
{
   [XmlElement(ElementName = "Devices")]
   public List<DewesoftSetup> dewesoftSiriusSetups { get; set; }

};


public class DewesoftSetup
{
   public double StartStoreTime;
   public int SampleRate;
   public int BlockSize;
   public int IBRate;
   public int AAFsr;
   public int MaxSampling;
   [XmlElement(ElementName = "Device")]
   public List<DeviceType> deviceType { get; set; }
};

public class DeviceType
{
   [XmlAttribute(AttributeName = "Type")]
   public string deviceType;

   [XmlElement(ElementName = "Slot")]
   public List<Slot> slot { get; set; }
};

public class Slot
{
   public string MeasuredValue { get; set; }
   public string Range { get; set; }
   public string LPFilter_Type { get; set; }
   public string LPFilter_Hz { get; set; }
   public string LPFilter_Order { get; set; }
   public string HPFilter_Hz { get; set; }
   public double AmplScale { get; set; }
   public double AmplOffset { get; set; }
   public string AmplShortInfo { get; set; }
   public string NamePrefix { get; set; }
   [XmlElement(ElementName = "OutputChannel")]
   public OutputChannel outputChannel;
   [XmlElement(ElementName = "AmplifierChainInfo/AmplifierInfo")]
   public AmplifierInfo ampInfo;
}

public class DewesoftCard
{
   public static string xmlFileName = @"C:\Dewesoft\SetupFile.xml";
   public double startStoreTime;
   public int sampleRate;
   public int blockSize;
   public int ibRate;
   public int aafSR;
   public int maxSampling;
   public int startZoomTime;
   public int endZoomTime;
};


public class OutputChannel
{
   public string Name { get; set; }
   public string Description { get; set; }
   public string Unit { get; set; }
   public int userScaleMin { get; set; }
   public int userScaleMax { get; set; }
   public int defaultComplexPresent { get; set; }
   public string Used { get; set; }
   public string Index { get; set; }
   public int DataType { get; set; }
   public double Scale { get; set; }
   public double Offset { get; set; }
   public double RangeMax { get; set; }
   public double RangeMin { get; set; }
   public int BitsLog { get; set; }
   public int SigBitCount { get; set; }
   public int BitsForMinMax { get; set; }
   public int BitCount { get; set; }
   public int ExportOrder { get; set; }
   public string MeasuredQuantity { get; set; }
   public int AmplifierScaleMin { get; set; }
   public int AmplifierScaleMax { get; set; }
};

public class AmplifierInfo
{
   public string Name { get; set; }
   public string Type { get; set; }
   public string SerialNr { get; set; }
   public string Revision { get; set; }
   public string Firmware { get; set; }
   public string CalDate { get; set; }
   public string Range { get; set; }
   public string LP_filter { get; set; }
   public string LP_filter_type { get; set; }
   public string LP_filter_order { get; set; }
   public string HP_filter { get; set; }
};

public static void readXML()
{
   XmlReader reader = XmlReader.Create(DewesoftCard.xmlFileName);

   reader.ReadToFollowing("DewesoftSetup");

   XmlSerializer serializer = new XmlSerializer(typeof(DewesoftDevices));
   DewesoftDevices setup = (DewesoftDevices)serializer.Deserialize(reader);
}

<?xml version="1.0" encoding="UTF-8"?>
<DataFileSetup>
    <System Name="Local">
        <SysInfo>
            <Software>
                <VersionCreated>X3 SP5 (RELEASE-181228) (64-bit)</VersionCreated>
                <VersionModifed>X3 SP5 (RELEASE-181228) (64-bit)</VersionModifed>
                <License_Type>Professional</License_Type>
            </Software>
         </SysInfo>
         <DewesoftSetup>
            <Devices>
                <StartStoreTime>43874.6969328704</StartStoreTime>
                <SampleRate>50000</SampleRate>
                <BlockSize>1000</BlockSize>
                <IBRate>10</IBRate>
                <AAFsr>21000</AAFsr>
                <MaxSampling>200000</MaxSampling>
                <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>
                        <HPFilter_Hz>1</HPFilter_Hz>
                        <OutMathChEnabled>8;1;0;0;0;0;0;0;0</OutMathChEnabled>
                        <OutputChannel>
                            <DisplayColor>#FFFF00</DisplayColor>
                            <Name>ACC-1X</Name>
                            <Description>Dome</Description>
                            <DefaultUnit>False</DefaultUnit>
                            <Unit>g</Unit>
                            <DefaultComplexPresent>1</DefaultComplexPresent>
                            <Used>True</Used>
                            <Index>AI;0</Index>
                            <DataType>4</DataType>
                            <Scale>200</Scale>
                            <RangeMax>2000</RangeMax>
                            <RangeMin>-2000</RangeMin>
                            <BitsLog>24</BitsLog>
                            <SigBitCount>19</SigBitCount>
                            <BitsForMinMax>24</BitsForMinMax>
                            <BitCount>24</BitCount>
                            <SIUnit>
                                <Label>g</Label>
                                <PhysicalQuantity>ACCELERATION</PhysicalQuantity>
                                <MetricPos>1</MetricPos>
                                <ImperialPos>1</ImperialPos>
                                <CustomPos>1</CustomPos>
                                <EditEnable>False</EditEnable>
                                <Scale>0.101972</Scale>
                            </SIUnit>
                            <MeasuredQuantity>ACCELERATION</MeasuredQuantity>
                            <OnlineInfo>
                                <IBStream Level="0"></IBStream>
                                <IBStream Level="1"></IBStream>
                                <IBStream Level="2"></IBStream>
                                <IBStream Level="3"></IBStream>
                                <IBStream Level="4"></IBStream>
                                <IBStream Level="5"></IBStream>
                            </OnlineInfo>
                            <AmplifierScaleMin>-10</AmplifierScaleMin>
                            <AmplifierScaleMax>10</AmplifierScaleMax>
                        </OutputChannel>
                        <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>
                </Device>
            </Devices>
        </DewesoftSetup>
  </System>
</DataFileSetup>

Есть несколько атрибутов "Device", но мне нужно тот, что находится под DewesoftSetup.

РЕДАКТИРОВАТЬ Я добавил больше документа xml. Я пытаюсь добавить тип устройства в список и слот для списка, так как их будет несколько.

1 Ответ

0 голосов
/ 14 февраля 2020

Попробуйте следующее:

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            readXML();
        }
        public static void readXML()
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            reader.ReadToFollowing("DewesoftSetup");

            XmlSerializer serializer = new XmlSerializer(typeof(DewesoftDevices));
            DewesoftDevices setup = (DewesoftDevices)serializer.Deserialize(reader);
        }
    }
    [XmlRoot(ElementName = "DewesoftSetup")]
    public class DewesoftDevices
    {
        [XmlElement(ElementName = "Devices")]
        public List<Devices> devices { get; set; }

    }


    public class Devices
    {
        public string StartStoreTime;
        public int SampleRate;
        public int IBRate;
        public int AAFsr;
        public int MaxSampling;

        [XmlElement(ElementName = "Device")]
        public List<DeviceType> deviceType { get; set; }
    }

    public class DeviceType
    {
        [XmlAttribute(AttributeName = "Type")]
        public string deviceType;

        [XmlElement(ElementName = "Slot")]
        public List<Slot> slot { get; set; }
    }
    public class Slot
    {
        public string Range { get; set; }
        public OutputChannel OutputChannel { get; set; }

        [XmlArray("AmplifierChainInfo")]
        [XmlArrayItem("AmplifierInfo")]
        public List<AmplifierInfo> AmplifierInfo { get; set; }
    }
    public class OutputChannel
    {
        public string Name { get; set; }
    }
    public class AmplifierInfo
    {
        public string Name { get; set; }
    }
}
...