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.
I guess that the format causes the problem. How can I fix it?
You copy the XML content. In VS, you choose Paste Special. VS will automatically converts the XML to proper C# classes.
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; }
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.