XmlSerializer 并粘贴 XML 为 类
XmlSerializer & Paste XML as classes
我有一个返回一些 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>
您的问题看起来与 to 的 Final Update 中描述的问题相同,即 xsd.exe
(因此 Paste XML as 类) 无法为包含重复元素的 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
. 的所有相关成员
示例 fiddle here.
我有一个返回一些 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>
您的问题看起来与 xsd.exe
(因此 Paste XML as 类) 无法为包含重复元素的 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
. 的所有相关成员
示例 fiddle here.