我有一个返回一些xml的Web服务。没有相应的xsd。
我在Visual Studio中使用选择性粘贴->粘贴XML作为类功能来生成要与XmlSerializer
一起使用的类:
代码执行该行时
--> XmlSerializer xmlSerialize = new XmlSerializer(typeof(table)); <--
table t = (table)xmlSerialize.Deserialize(new StringReader(soapResult));
它引发异常:
System.InvalidOperationException: 'Unable to generate a temporary class (result=1).
error CS0030: Cannot convert type 'string[]' to 'string'
error CS0029: Cannot implicitly convert type 'string' to 'string[]'
生成的类如下:
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class table
{
private string[] labelField;
private string[] classnameField;
private string[] datatypeField;
private string[][] rowField;
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("d", IsNullable = false)]
public string[] label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("d", IsNullable = false)]
public string[] classname
{
get
{
return this.classnameField;
}
set
{
this.classnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("d", IsNullable = false)]
public string[] datatype
{
get
{
return this.datatypeField;
}
set
{
this.datatypeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlArrayItemAttribute("d", typeof(string), IsNullable = true)]
public string[][] row
{
get
{
return this.rowField;
}
set
{
this.rowField = value;
}
}
}
问题出在行字段,其他行都可以。 如果我改变
[System.Xml.Serialization.XmlArrayItemAttribute("d", typeof(string), IsNullable = true)]
到
[System.Xml.Serialization.XmlArrayItemAttribute("d", typeof(string[]), IsNullable = true)]
错误消失了,但是我的对象只有一个空字符串数组。
要反序列化(为了简化而压缩)的XML数据如下
<?xml version="1.0" encoding="UTF-8"?>
<table>
<label>
<d>JobDefinition.SearchName</d>
<d>Job.JobDefinition</d>
</label>
<classname>
<d>JobDefinition.SearchName</d>
<d>Job.JobDefinition</d>
</classname>
<datatype>
<d>String</d>
<d>obj.JobDefinition</d>
</datatype>
<row>
<d>AB_DEFG_QA_RUN</d>
<d>A_JOB_Run</d>
</row>
<row>
<d>AB_DEFG_QA_RUN</d>
<d>B_JOB_Run</d>
</row>
</table>
答案 0 :(得分:0)
您的问题看上去与从this answer到How to serialize List<List<object>>
?的最终更新中描述的问题相同,即xsd.exe
(因此粘贴XML例如类)在为包含重复元素的XML推断正确的XML数据模型时遇到麻烦,这些重复元素包含嵌套的重复元素,此处为<row>
和<d>
:
<table>
<row>
<d>Value of repeating element inside a repeating element.</d>
<d>Value of repeating element inside a repeating element.</d>
</row>
<row>
<d>Value of repeating element inside a repeating element.</d>
<d>Value of repeating element inside a repeating element.</d>
</row>
</table>
该答案的建议是使用其他代码生成工具或手动构造类。我将您的XML上传到https://xmltocsharp.azurewebsites.net/,在合并重复的类之后,获得了以下数据模型:
[XmlRoot(ElementName = "row")]
public class Row
{
[XmlElement(ElementName = "d")]
public List<string> D { get; set; }
}
[XmlRoot(ElementName = "table")]
public class Table
{
[XmlElement(ElementName = "label")]
public Row Label { get; set; }
[XmlElement(ElementName = "classname")]
public Row Classname { get; set; }
[XmlElement(ElementName = "datatype")]
public Row Datatype { get; set; }
[XmlElement(ElementName = "row")]
public List<Row> Row { get; set; }
}
使用此方法,我可以成功地反序列化和重新序列化XML,而不会丢失数据。注意:
Label
,Classname
,Datatype
和Row
生成了截然不同但相同的类。我决定将它们全部合并为单个Row
类型,并将其用于Table
的所有相关成员。提琴here。