这是我试图反序列化的 XML。我不断收到“resultConfig xmlns=' '> 不是预期的。”}
关于这个问题,我厌倦了 stackoverflow 上的所有解决方案,但都没有真正奏效。先感谢您。
<?xml version="1.0" encoding="UTF-8" ?>
<resultConfig>
<folders>
<folder>
<location>C:\</location>
</folder>
<folder>
<location>C:\Temp</location>
</folder>
</folders>
<resultClasses>
<resultClass name="ScdExctractSqlXYZ" type="CSV">
<resultTypeQuery>REGEX</resultTypeQuery>
<testIdQuery>REGEX</testIdQuery>
</resultClass>
<resultClass name="SubNotification" type="XML">
<resultTypeQuery>XPATH</resultTypeQuery>
<testIdQuery>XPATH</testIdQuery>
<transformation>something.xsl</transformation>
</resultClass>
</resultClasses>
</resultConfig>
我将我的类对象声明为:
namespace TestGen
{
public class ResultClasses
{
[XmlRoot("resultConfig")]
public class resultConfig
{
[XmlArray("folders")]
[XmlArrayItem("folder")]
public List<folder> folders { get; set; }
[XmlArray("resultClasses")]
[XmlArrayItem("resultClass")]
public List<resultClass> classes { get; set; }
}
public class folder
{
[XmlArray("location")]
public string location { get; set; }
}
public class resultClass
{
[XmlAttribute("name")]
public string name { get; set; }
[XmlAttribute("type")]
public string type { get; set; }
[XmlArrayItem("resultTypeQuery")]
public string resultTypeQuery { get; set; }
[XmlArrayItem("testIdQuery")]
public string testIdQuery { get; set; }
[XmlArrayItem("transformation")]
public string transformation { get; set; }
}
}
}
然后我以这种方式调用我的反序列化器:
using (FileStream reader = new FileStream(@"C:\ResultConfig.xml", FileMode.Open, FileAccess.Read))
{
XmlSerializer serializer = new XmlSerializer(typeof(ResultClasses));
var ei = serializer.Deserialize(reader);
}