0

I am trying to read an XML file which is generated by Matlab (a XML representation of Simulink Model). For the sake of question I have shortened it. Below is the XML.

<?xml version="1.0" encoding="utf-8"?>
<ModelInformation Version="1.0">
  <Model Name="MatlabToJenkinsIntegrationSample">
    <P Name="Version">9.0</P>
    <P Name="SavedCharacterEncoding">windows-1252</P>
    <GraphicalInterface>
      <P Name="NumRootInports">3</P>
      <Inport Name="Input1">
        <P Name="BusObject"/>
        <P Name="OutputFunctionCall">off</P>
        <P Name="SampleTime">-1</P>
        <P Name="UnitExpr">inherit</P>
      </Inport>
      <Inport Name="Input2 ">
        <P Name="BusObject"/>
        <P Name="OutputFunctionCall">off</P>
        <P Name="SampleTime">-1</P>
        <P Name="UnitExpr">inherit</P>
      </Inport>
      <Inport Name="MultiplicationFactor">
        <P Name="BusObject"/>
        <P Name="OutputFunctionCall">off</P>
        <P Name="SampleTime">-1</P>
        <P Name="UnitExpr">inherit</P>
      </Inport>
      <P Name="NumRootOutports">1</P>
      <Outport Name="Result">
        <P Name="BusObject"/>
        <P Name="BusOutputAsStruct">off</P>
        <P Name="UnitExpr">inherit</P>
      </Outport>
      <P Name="ParameterArgumentNames"/>
      <P Name="ComputedModelVersion">1.4</P>
      <P Name="NumModelReferences">0</P>
      <P Name="NumTestPointedSignals">0</P>       
    </GraphicalInterface>
  </Model>
</ModelInformation>

I had a look at it and it gave a clear Class and it's properties(for me). So I generated the classes as shown below

public class Model
{
    public string Name { get; set; }

    public string Version { get; set; }

    public string SavedCharacterEncoding { get; set; }

    public GraphicalInterface TheGraphicalInterface { get; set; }
}

public class GraphicalInterface
{
    public List<InPort> InPorts { get; set; }

    public List<OutPort> OutPorts { get; set; }
}

And Similarly for InPort and OutPort. Now the question is that how do I parse it into these classes. I can not wrap my head around it. Creating classes looks logical but after that I am not able to progress at all.

Please provide the input and a solution. There are no values as there in normal XML file. I have looked at certain examples but not able to find a solution.

Abhishek Kumar
  • 342
  • 5
  • 22
  • I would start here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/introducing-xml-serialization – Alex Paven Jan 15 '18 at 16:41
  • If you want to store your object as xml and retrieve it back as object, then you can just serialise and de- serialise your object. Have a look at http://www.c-sharpcorner.com/article/serialization-and-deserialization-in-c-sharp/ – Vinay Pandey Jan 15 '18 at 16:43
  • It is already in XML. I need objects from there @VinayPandey – Abhishek Kumar Jan 15 '18 at 16:47
  • 1
    Hi, have a look at this link https://stackoverflow.com/questions/48187967/how-can-i-deserialize-a-xml-with-this-kind-of-structure-into-its-proper-poco-mo/48231364#48231364 this is something similar you are looking at – Maddy Jan 15 '18 at 16:48
  • @AbhishekKumar, refer to the link it has part covering deserialisation as well. – Vinay Pandey Jan 15 '18 at 16:51
  • @VinayPandey Please have a look at the XML. The deserialisation is not clear. – Abhishek Kumar Jan 15 '18 at 17:15

0 Answers0