0

I have below method which has to take all the input parameter name and create an XML for key and value. Please help me to fix this

This question is not duplicate because i don't want to create any new class when i already have system classes such as dictionary,hash table and key value pair. Let me know if there is any solution without creating new class.

My expected result to be is

<?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>

But I am getting the output below, without key and value:

<?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>

Here is my method that serializes the 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);
        }
    }
rouen
  • 5,003
  • 2
  • 25
  • 48
Sanju Rao
  • 331
  • 1
  • 6
  • 25
  • `KeyValuePair` is not serializable. Here is exactly the same [question](http://stackoverflow.com/questions/2658916/serializing-a-list-of-key-value-pairs-to-xml) – Andrei Jun 27 '14 at 12:46
  • @L.B i have edited the question which differentiates and say they are not duplicate. – Sanju Rao Jun 27 '14 at 14:00

0 Answers0