1

I serialize a Vehicle object with the following code to serialize the object:

        XmlSerializer serializer = new XmlSerializer(typeof(Vehicle));
        using (StreamWriter sw = new StreamWriter(RESULTS_FILE_PATH_))
        using (XmlWriter writer = new XmlTextWriter(sw))
        {
            try
            {
                serializer.Serialize(writer, vehicles[0]);
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception thrown: " + exception);
            }
        }

The results are as follows:

<?xml version="1.0" encoding="utf-8"?>
    <Model xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
        <modelYear xmlns="urn:configcompare4g.kp.chrome.com">2014</modelYear> 

            <subdivisionName xmlns="urn:configcompare4g.kp.chrome.com">Buick</subdivisionName><modelId 

    </Model>

I need the format to be like this:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
  <S:Body>
    <ModelArrayElement xmlns="urn:configcompare4g.kp.chrome.com">
     <model>
        <modelYear>2014</modelYear>
        <subdivisionName>Buick</subdivisionName>
     </model>
    </ModelArrayElement>
  </S:Body>
 </S:Envelope>

Does anyone have any suggestions on how I can produce the proper format?

1
  • 1
    This is still basically XML, try experimenting with XElement and how you output the names of them. I will go get the code from my other computer and post an answer shortly - I did exactly this last year. Commented Dec 11, 2013 at 23:07

2 Answers 2

4

For such a serialization need to use SoapFormatter. Detailed information can be found in the description on MSDN (SoapFormatter).

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately this did not work for me. The results produced the following format: <SOAP-ENV:Envelope xmlns:xsi="w3.org/2001/XMLSchema-instance" xmlns:xsd="....etc. Which is not what I am looking for.
-1

If your XML output is non-standard and it will be difficult to use any default formatter, I would think about using some templating engine, like RazorEngine, so it would be something like:

string template = 
@"<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">"+
  "<S:Body>"+
  "   <ModelArrayElement xmlns="urn:configcompare4g.kp.chrome.com">"+
  "      <model>"+
  "         <modelYear>@Model.modelYear</modelYear>"+
  "         <subdivisionName>@Model.subdivisionName</subdivisionName>"+
  "      </model>"+
  "    </ModelArrayElement>"+
  "</S:Body>"+
  "</S:Envelope>";

var model = vehicles[0];
string result = Razor.Parse(template, model);

This gives a complete control of output and allows to make more complex logic based on loops, conditionals etc.

1 Comment

@DStanley, you will write a few templates then. OP does not mention how many models exists and how complicated they are. I am using such solution in my software because Razor allows you to use more complicated logic like loops and conditions. In my opinion this suggestion does not deserve down vote.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.