0

I hope you have a great day!

I am now struggling to parse XML (especially for CPLEX Sol file formatted by XML) to C# class.

I defined a class as shown below.

[XmlRoot("CPLEXSolutions")]
public class CplexSol
{
    [XmlElement("CPLEXSolution")]
    public List<CPLEXSolution> Solutions { get; set; }

    public CplexSol()
    {
        Solutions = new List<CPLEXSolution>();
    }
}
public class CPLEXSolution
{
    [XmlElement("header")]
    public string Header { get; set; }
    [XmlElement("variables")]
    public List<CplexVariable> CplexVariables { get; set; }

    public CPLEXSolution()
    {
        CplexVariables = new List<CplexVariable>();
    }
}

public class CplexVariable
{
    [XmlElement("name")]
    public string name { get; set; }
    [XmlElement("index")]
    public string index { get; set; }
    [XmlElement("value")]
    public string value { get; set; }
}

The XML format of the file is somehow different with a standard format as shown below.

<?xml version = "1.0" encoding="UTF-8" standalone="yes"?>
<CPLEXSolutions version="1.2">
 <CPLEXSolution version="1.2">
  <header
    problemName="ILOG.CPLEX"
    solutionName="m2"
    solutionIndex="1"
    MIPStartEffortLevel="0"
    writeLevel="2"/>
  <variables>
   <variable name="X_0_1" index="0" value="0"/>
   <variable name="X_1_0" index="1" value="1"/>
   <variable name="X_0_2" index="2" value="1"/>
   <variable name="X_2_0" index="3" value="0"/>
   <variable name="X_0_3" index="4" value="1"/>
   <variable name="X_3_0" index="5" value="0"/>
   <variable name="X_0_4" index="6" value="1"/>
....
  </variables>
 </CPLEXSolution>

Do you have any idea to parse it into the defined class? Thanks in advance!

[Update] I tested the code that @jdweng suggested but the class is empty after running it as shown below.

enter image description here

I guess that the format causes the problem. How can I fix it?

Thomas J
  • 35
  • 9
  • I just tested the code a 2nd time and it is giving results. Did you run exactly my code including the classes? – jdweng Dec 12 '19 at 10:25
  • JUst wondering why you would ever want to do this? Why not just connect to the CPLEX solver in C#, define and solve your problem and then get the solution directly? – TimChippingtonDerrick Dec 12 '19 at 12:20

2 Answers2

0

You copy the XML content. In VS, you choose Paste Special. VS will automatically converts the XML to proper C# classes.

Antediluvian
  • 653
  • 1
  • 6
  • 18
0

I fixed the code :

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)
        {
            XmlReader reader = XmlReader.Create(FILENAME);

            XmlSerializer serializer = new XmlSerializer(typeof(CplexSol));
            CplexSol cplexsol = (CplexSol)serializer.Deserialize(reader);

        }
    }
    [XmlRoot("CPLEXSolutions")]
    public class CplexSol
    {
        [XmlElement("CPLEXSolution")]
        public List<CPLEXSolution> Solutions { get; set; }
    }
    public class CPLEXSolution
    {
        [XmlElement("header")]
        public string Header { get; set; }
        [XmlArray("variables")]
        [XmlArrayItem("variable")]
        public List<CplexVariable> CplexVariables { get; set; }
    }

    public class CplexVariable
    {
        [XmlAttribute("name")]
        public string name { get; set; }
        [XmlAttribute("index")]
        public string index { get; set; }
        [XmlAttribute("value")]
        public string value { get; set; }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20