序列化键值对时。内部数据未被序列化

时间:2014-06-27 12:44:11

标签: c# xml

我有以下方法,它必须获取所有输入参数名称并为键和值创建XML。请帮我解决这个问题

这个问题不重复,因为当我已经拥有系统类(如字典,哈希表和键值对)时,我不想创建任何新类。如果没有创建新课程,请告诉我是否有任何解决方案。

我的预期结果是

<?xml version="1.0" encoding="utf-8"?>
<ArrayOfKeyValuePairOfStringString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <KeyValuePairOfStringString>
        <key>abc</key>
        <value>1</value>
    </KeyValuePairOfStringString>
    <KeyValuePairOfStringString>
        <key>xyz</key>
        <value>2</value>
    </KeyValuePairOfStringString>
</ArrayOfKeyValuePairOfStringString>

但是我得到了下面的输出,没有键和值:

<?xml version="1.0" encoding="utf-8"?>
    <ArrayOfKeyValuePairOfStringString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <KeyValuePairOfStringString/>
        <KeyValuePairOfStringString/>   
    </ArrayOfKeyValuePairOfStringString>

这是我的序列化XML的方法:

public void LogSpParameters(List<IDbDataParameter> parameters)
    {
        var inputParameters = parameters.FindAll(x => x.Direction == ParameterDirection.Input);
        List<KeyValuePair<string,string>> inputParameterListObj = new List<KeyValuePair<string,string>>();
        foreach (var dataParameter in inputParameters)
        {
            inputParameterListObj.Add(new KeyValuePair<string,string>
            (
                Convert.ToString(dataParameter.ParameterName),
                Convert.ToString(dataParameter.Value)
            ));
        }

        using (MemoryStream xml = new MemoryStream())
        {
            var serializer = new XmlSerializer(typeof(KeyValuePair<string, string>));
            serializer.Serialize(xml, inputParameterListObj);
            xml.Seek(0, 0);
            byte[] bytes = xml.GetBuffer();
            string serialized = system.Text.Encoding.ASCII.GetString(bytes);
        }
    }

0 个答案:

没有答案