[英]Deserialize xml with different format in C#
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.我现在正在努力将 XML(特别是对于由 XML 格式化的 CPLEX Sol 文件)解析为 C# 类。
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 格式与标准格式有些不同,如下所示。
<?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. [更新] 我测试了@jdweng 建议的代码,但运行后该类为空,如下所示。
I guess that the format causes the problem.我猜是格式导致了问题。 How can I fix it?我该如何解决?
You copy the XML content.您复制 XML 内容。 In VS, you choose Paste Special.在 VS 中,您选择“选择性粘贴”。 VS will automatically converts the XML to proper C# classes. VS 会自动将 XML 转换为正确的 C# 类。
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; }
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.