XMLSerializer 异常 "There was an error reflecting field" 和 "For non-array types, you may use the following attributes: XmlAttribute, .."

XMLSerializer exceptions "There was an error reflecting field" and "For non-array types, you may use the following attributes: XmlAttribute, .."

我是新手,请耐心等待。 我需要将 JSON 字符串转换为 XML 进行一些修改 来自

{"computerid":123456,"computername":"mycomputer","computermodel":"mymodel"}

<?xml version="1.0"?>
<SqlMultiFilter xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Parameter>
<Filter>
    <ParamName>computerid</ParamName>
    <ParamValues>
        <ParamValue>123456</ParamValue>
    </ParamValues>
</Filter>
<Filter>
    <ParamName>computername</ParamName>
    <ParamValues>
        <ParamValue>mycomputer</ParamValue>
    </ParamValues>
</Filter>
<Filter>
    <ParamName>computermodel</ParamName>
    <ParamValues>
        <ParamValue>mymodel</ParamValue>
    </ParamValues>
</Filter>
</Parameter>
</SqlMultiFilter>

我用那种方式创建了一个publicclass

[XmlRoot("SQLMultiFilter", Namespace = "http://www.cpandl.com",
    IsNullable = false)]
    public class SQLMultiFilter
    {
        [XmlArrayAttribute("Parameter")]
        public string ParamName;
        [XmlArrayAttribute("ParamValues")]
        public string ParamValue;
    }

当我 post 我的 json 我在这方面遇到了错误

XmlSerializer serializer = new XmlSerializer(typeof(SQLMultiFilter));

错误

Inner Exception 1:
InvalidOperationException: There was an error reflecting field 'ParamName'.

Inner Exception 2:
InvalidOperationException: For non-array types, you may use the following attributes: XmlAttribute, XmlText, XmlElement, or XmlAnyElement.

我哪里出错了?

提前致谢

[XmlRoot("SQLMultiFilter", Namespace = "http://www.cpandl.com", IsNullable = false)]
public class SqlMultiFilter
{
    public List<Filter> Parameter { get; set; }
}

public class Filter
{
    public string ParamName;

    [XmlArrayItem("ParamValue")]
    public List<string> ParamValues { get; set; }
}

这两个 类 将允许您重现所示架构的 XML。

var serializer = new XmlSerializer(typeof(SqlMultiFilter));

var sqlMultiFilter = new SqlMultiFilter
{
    Parameter = new List<Filter>
    {
        new Filter { ParamName = "computerid", ParamValues = new List<string> { "123456" } },
        new Filter { ParamName = "computername", ParamValues = new List<string> { "mycomputer" } },
        new Filter { ParamName = "computermodel", ParamValues = new List<string> { "mymodel" } }
    }
};

var settings = new XmlWriterSettings { Indent = true };

using (var xmlWriter = XmlWriter.Create(Console.Out, settings))
    serializer.Serialize(xmlWriter, sqlMultiFilter);

会得到想要的结果。