У меня есть веб-сервис, который возвращает XML.Нет соответствующего xsd.
Я использовал Paste Special -> Paste XML как функциональность классов в Visual Studio, чтобы сгенерировать классы для использования с 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>