Reputation: 21
I can't deserialize the xml-File below: how to do?
some infos:
XML-File:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Configurator>
<Machine OrderNumber="123456" name="ProjectABC" posindex="null" position="G" revision="01">
<GeoVar/>
<IfcProp/>
<Box assemblyCode="GGA" assemblyIndex="01" los="BoxLos" losindex="01" name="BoxName" classnumber="175892" type="BOXType">
<GeoVar>
<D1>2000.0</D1>
<D2>2400.0</D2>
<D5>2000.0</D5>
<D7>1</D7>
<Start_x_Coordinate>0</Start_x_Coordinate>
<Start_y_Coordinate>0</Start_y_Coordinate>
<Start_z_Coordinate>500</Start_z_Coordinate>
<inSide>6</inSide>
</GeoVar>
<IfcProp/>
<Component assemblyCode="FTE" assemblyIndex="01" name="Filter" classnumber="171115" type="FILTER">
<GeoVar>
<D5>1894.0</D5>
<D4>1894.0</D4>
<D16>600.0</D16>
<B>1894.0</B>
<H>1894.0</H>
<T>600.0</T>
<BA>1894.0</BA>
<HA>1894.0</HA>
<TA>600.0</TA>
<Start_x_Coordinate>53</Start_x_Coordinate>
<Start_y_Coordinate>53</Start_y_Coordinate>
<Start_z_Coordinate>1153</Start_z_Coordinate>
<Layout>1</Layout>
<AirDirection>1</AirDirection>
<inSide>6</inSide>
<D6>70.0</D6>
<D7>64.0</D7>
<D8>32.0</D8>
<D9>610.0</D9>
<D10>610.0</D10>
<D12>3</D12>
<D13>3</D13>
<D19>2</D19>
<D20>2</D20>
</GeoVar>
<IfcProp>
<Typ>592x592x600x8 </Typ>
<Volume>1.777,8 m³/h</Volume>
</IfcProp>
</Component>
<Component assemblyCode="" assemblyIndex="01" name="RevChambre" classnumber="000000" type="AUDITCHAMBER">
<GeoVar>
<B>1894.0</B>
<H>1900.0</H>
<T>600.0</T>
<B>1894.0</B>
<H>1900.0</H>
<T>600.0</T>
<BA>1894.0</BA>
<HA>1900.0</HA>
<TA>600.0</TA>
<Start_x_Coordinate>53</Start_x_Coordinate>
<Start_y_Coordinate>53</Start_y_Coordinate>
<Start_z_Coordinate>553</Start_z_Coordinate>
<Layout>1</Layout>
<SubComponent assemblyCode="GGT" name="Door" classnumber="196387">
<D1>1900.0</D1>
<D2>600.0</D2>
<D5>1200</D5>
<D10>2</D10>
<D16>1</D16>
<D20>3</D20>
<D21>2</D21>
<D23>2</D23>
<D24>2</D24>
<D25>2</D25>
<D50>2</D50>
</SubComponent>
<SubComponent assemblyCode="GGT" name="Door" classnumber="196387">
<D1>1500.0</D1>
<D2>600.0</D2>
<D5>1200</D5>
<D10>1</D10>
<D16>1</D16>
<D20>2</D20>
<D21>2</D21>
<D23>1</D23>
<D24>2</D24>
<D25>2</D25>
<D50>0</D50>
</SubComponent>
<SubComponent assemblyCode="WAS" name="WESS" classnumber="169683">
<D4>600.0</D4>
<D5>1894.0</D5>
<D6>46.0</D6>
<D7>6</D7>
<D9>1</D9>
<D10>1</D10>
<D13>1</D13>
</SubComponent>
<AirDirection>1</AirDirection>
</GeoVar>
<IfcProp/>
</Component>
<Component assemblyCode="" assemblyIndex="02" name="RevChambre 2" classnumber="000000" type="AUDITCHAMBER">
<GeoVar>
<B>1894.0</B>
<H>1900.0</H>
<T>1474.0</T>
<B>1894.0</B>
<H>1900.0</H>
<T>1474.0</T>
<BA>1894.0</BA>
<HA>1900.0</HA>
<TA>1474.0</TA>
<Start_x_Coordinate>53</Start_x_Coordinate>
<Start_y_Coordinate>53</Start_y_Coordinate>
<Start_z_Coordinate>1373</Start_z_Coordinate>
<Layout>1</Layout>
<SubComponent assemblyCode="GGT" name="Door" classnumber="196387">
<D1>1900.0</D1>
<D2>600.0</D2>
<D5>1200</D5>
<D10>1</D10>
<D16>1</D16>
<D20>3</D20>
<D21>2</D21>
<D23>2</D23>
<D24>2</D24>
<D25>2</D25>
<D50>2</D50>
</SubComponent>
<SubComponent assemblyCode="GGT" name="Door" classnumber="196387">
<D1>1500.0</D1>
<D2>600.0</D2>
<D5>1200</D5>
<D10>2</D10>
<D16>1</D16>
<D20>2</D20>
<D21>2</D21>
<D23>1</D23>
<D24>1</D24>
<D25>2</D25>
<D50>2</D50>
</SubComponent>
<SubComponent assemblyCode="WAS" name="Wa" classnumber="169683">
<D4>1474.0</D4>
<D5>1894.0</D5>
<D6>46.0</D6>
<D7>6</D7>
<D9>1</D9>
<D10>1</D10>
<D13>2</D13>
</SubComponent>
<AirDirection>1</AirDirection>
</GeoVar>
<IfcProp/>
</Component>
</Box>
<Component assemblyCode="AKA" assemblyIndex="01" name="Pipe" classnumber="185087" type="DUCTJOIN">
<GeoVar>
<D2>1000.0</D2>
<D1>1000.0</D1>
<D3>500.0</D3>
<B>1000.0</B>
<H>1000.0</H>
<T>500.0</T>
<BA>1000.0</BA>
<HA>1000.0</HA>
<TA>500.0</TA>
<Start_x_Coordinate>947</Start_x_Coordinate>
<Start_y_Coordinate>490</Start_y_Coordinate>
<Start_z_Coordinate>0</Start_z_Coordinate>
<Layout>1</Layout>
<inOut>1</inOut>
<AirDirection>1</AirDirection>
<inSide>6</inSide>
</GeoVar>
<IfcProp>
<Drag>0 Pa</Drag>
<certificat>No </certificat>
<velocity>0,3 m/s</velocity>
<Volume>1.000 m³/h</Volume>
</IfcProp>
</Component>
<Component assemblyCode="AKA" assemblyIndex="02" name="Pipe 2" classnumber="185087" type="DUCTJOIN">
<GeoVar>
<D2>1000.0</D2>
<D1>1894.0</D1>
<D3>500.0</D3>
<B>1000.0</B>
<H>1894.0</H>
<T>500.0</T>
<BA>1000.0</BA>
<HA>1894.0</HA>
<TA>500.0</TA>
<Start_x_Coordinate>947</Start_x_Coordinate>
<Start_y_Coordinate>53</Start_y_Coordinate>
<Start_z_Coordinate>2900</Start_z_Coordinate>
<Layout>1</Layout>
<inOut>2</inOut>
<AirDirection>1</AirDirection>
<inSide>6</inSide>
</GeoVar>
<IfcProp>
<Drag>0 Pa</Drag>
<certificat>No </certificat>
<velocity>2,3 m/s</velocity>
<Volume>16.000 m³/h</Volume>
</IfcProp>
</Component>
<Component assemblyCode="JAK" assemblyIndex="01" name="Jalousie" classnumber="185931" type="MULTILEAFDAMPER">
<GeoVar>
<D1>476.0</D1>
<D2>1000.0</D2>
<D4>100.0</D4>
<B>476.0</B>
<H>1000.0</H>
<T>100.0</T>
<BA>476.0</BA>
<HA>1000.0</HA>
<TA>100.0</TA>
<Start_x_Coordinate>196</Start_x_Coordinate>
<Start_y_Coordinate>53</Start_y_Coordinate>
<Start_z_Coordinate>400</Start_z_Coordinate>
<Layout>1</Layout>
<inOut>1</inOut>
<AirDirection>1</AirDirection>
<inSide>6</inSide>
</GeoVar>
<IfcProp>
<Count>0 </Count>
<Wall>wa</Wall>
<Lame>la</Lame>
<Cadre>Or</Cadre>
<Volume>15.000 m³/h</Volume>
</IfcProp>
</Component>
</Machine>
</Configurator>
What i have tried:
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace HelperTest
{
public class Tests
{
[Test]
public void Machine()
{
string file = @"Directory\file.xml";
Deserialize(file);
}
public void Deserialize(string file)
{
List<Machine> MachineList = new List<Machine>();
try
{
XmlSerializer serializer = new XmlSerializer(typeof(Machine));
using (TextReader reader = new StringReader(System.IO.File.ReadAllText(file)))
{
Machine result = (Machine)serializer.Deserialize(reader);
MachineList.Add(result);
}
}
catch (Exception ex)
{
throw new InvalidOperationException("Failed to create object from xml string", ex);
}
}
}
[Serializable, XmlRoot(ElementName = "Machine")]
public class Machine : AttributesSet
{
// elements
[XmlElement(ElementName = "GeoVar")]
public GeoVar geoVar { get; set; }
//
[XmlElement(ElementName = "IfcProp")]
public IFCProp ifcProp { get; set; }
//
[XmlElement(ElementName = "BOX")]
public List<BOX> box { get; set; }
//
[XmlElement(ElementName = "Component")]
public List<Component> component { get; set; }
}
public class AttributesSet
{
//all attributes are optional
[XmlAttribute(AttributeName = "name")]
string name { get; set; }
[XmlAttribute(AttributeName = "classnumber")]
string classnumber { get; set; }
[XmlAttribute(AttributeName = "OrderNumber")]
public string OrderNumber { get; set; }
[XmlAttribute(AttributeName = "position")]
public string position { get; set; }
[XmlAttribute(AttributeName = "posindex")]
public string posIndex { get; set; }
[XmlAttribute(AttributeName = "revision")]
public string revision { get; set; }
[XmlAttribute(AttributeName = "assemblyIndex")]
ushort assemblyIndex { get; set; }
[XmlAttribute(AttributeName = "assemblyCode")]
ushort assemblyCode { get; set; }
[XmlAttribute(AttributeName = "type")]
string type { get; set; }
}
[Serializable, XmlRoot(ElementName = "BOX")]
public class BOX : AttributesSet
{
// elements
[XmlElement(ElementName = "GeoVar")]
public GeoVar geoVar { get; set; }
[XmlElement(ElementName = "IfcProp")]
public IFCProp ifcProp { get; set; }
[XmlElement(ElementName = "Component")]
public List<Component> component { get; set; }
}
[Serializable, XmlRoot(ElementName = "GeoVar")]
public class GeoVar
{
[XmlElement(ElementName = "Start_x_Coordinate")]
public string start_x_Koordinate
{
get; set;
}
//
[XmlElement(ElementName = "Start_y_Coordinate")]
public string start_y_Coordinate
{
get; set;
}
//
[XmlElement(ElementName = "Start_z_Coordinate")]
public string start_z_Coordinate
{
get; set;
}
[XmlElement(ElementName = "B")]
double B { get; set; }
//
[XmlElement(ElementName = "H")]
double H { get; set; }
//
[XmlElement(ElementName = "T")]
double T { get; set; }
[XmlElement(ElementName = "BA")]
double BA { get; set; }
//
[XmlElement(ElementName = "HA")]
double HA { get; set; }
//
[XmlElement(ElementName = "TA")]
double TA { get; set; }
[XmlElement(ElementName = "AirDirection")]
ushort airDirection { get; set; }
//
[XmlElement(ElementName = "Layout")]
ushort layout { get; set; }
//
//
[XmlElement(ElementName = "inSide")]
ushort inSide { get; set; }
//
[XmlElement(ElementName = "inOut")]
ushort inOut { get; set; }
[XmlElement(ElementName = "SubComponent")]
public List<SubComponent> subComponent { get; set; }
}
[Serializable, XmlRoot(ElementName = "IfcProp")]
public class IFCProp
{
// TODO: How to manage a varietie of Elements?
}
[Serializable, XmlRoot(ElementName = "SubComponent")]
public class SubComponent : AttributesSet
{
// elements
[XmlElement(ElementName = "GeoVar")]
public GeoVar geoVar { get; set; }
//
[XmlElement(ElementName = "IfcProp")]
public IFCProp ifcProp { get; set; }
}
[Serializable, XmlRoot(ElementName = "Component")]
public class Component : AttributesSet
{
// elements
[XmlElement(ElementName = "GeoVar")]
public GeoVar geoVar { get; set; }
//
[XmlElement(ElementName = "IfcProp")]
public IFCProp ifcProp { get; set; }
[XmlElement(ElementName = "SubComponent")]
public List<SubComponent> subComponent { get; set; }
}
}
Upvotes: 0
Views: 62
Reputation: 866
You are missing root element class i.e. Configurator
[Serializable, XmlRoot(ElementName = "Configurator")]
public class Configurator
{
[XmlElement(ElementName = "Machine")]
public Machine Machine { get; set; }
}
And while de-serializing use Configurator
instead of Machine
.
XmlSerializer serializer = new XmlSerializer(typeof(Configurator));
using (TextReader reader = new StringReader(System.IO.File.ReadAllText(file)))
{
Configurator result = (Configurator)serializer.Deserialize(reader);
MachineList.Add(result.Machine);
}
Upvotes: 2