1

Im developing a web api in C#. The web api should return an xml like:

<personDatas>
 <personData>
   <affdatalist>
     <object1> information </object1>
     <object2> information </object2>
     <object3> information </object3>
   </affdatalist>
   <anotherObject1> infooo </anotherObject1>
   <anotherObject2> infooo </anotherObject2>
 </personData>
</personDatas>

The xml can have 1 to many personData elements and the personData element can have 1 to many affdatalist elements.

What would be the best practice to generate such an XML in a web api using C# 6?

Ive tried with XSD based on a schema definition.

Any help would be greatly appreciated.

4 Answers 4

3

You can use the Serialize method to generate this xml from the model it self.

public string SerializeXml<T>(T config) 
        {
            XmlSerializer xsSubmit = new XmlSerializer(typeof(T));
            string xml = "";
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = false;

            using (var sww = new StringWriter())
            {
                using (XmlWriter writer = XmlWriter.Create(sww, settings))
                {
                    xsSubmit.Serialize(writer, config);
                    xml = sww.ToString();
                }
            }

            return xml;
        }

This will return XML string , you need the model which is similar to your required XML. This is what i have created as model from your XML that you can change as per your requiment.

 [XmlRoot(ElementName = "affdatalist")]
    public class Affdatalist
    {
        [XmlElement(ElementName = "object1")]
        public string Object1 { get; set; }
        [XmlElement(ElementName = "object2")]
        public string Object2 { get; set; }
        [XmlElement(ElementName = "object3")]
        public string Object3 { get; set; }
    }

    [XmlRoot(ElementName = "personData")]
    public class PersonData
    {
        [XmlElement(ElementName = "affdatalist")]
        public Affdatalist Affdatalist { get; set; }
        [XmlElement(ElementName = "anotherObject1")]
        public string AnotherObject1 { get; set; }
        [XmlElement(ElementName = "anotherObject2")]
        public string AnotherObject2 { get; set; }
    }

    [XmlRoot(ElementName = "personDatas")]
    public class PersonDatas
    {
        [XmlElement(ElementName = "personData")]
        public PersonData PersonData { get; set; }
    }

You can use as sample below

PersonDatas data = new PersonDatas();
var xml = this.SerializeXml<PersonDatas>(data); // your model with data
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks alot! this helped. But in order to manage multiple elements of personData and then multiple elements of affdatalist's i personData i added List's in in each of the classes and then saving the object created in the loop in the list.
1

This is my sample after using what Sulay Shah said:

PersonDatas data = new PersonDatas();
            for (int x = 0; x < 2; x++)
            {

                data.PersonData = new PersonData();
                for (int i = 0; i < 2; i++)
                {
                    Affdatalist affdata = new Affdatalist();
                    affdata.Object1 = "LALALALALLALA";
                    affdata.Object2 = "lqlqlqlqlqlqlql";
                    affdata.Object3 = "ililililililililililil";
                    data.PersonData.Affdatalist.Add(affdata);
                }

                data.PersonData.AnotherObject1 = "ali";
                data.PersonData.AnotherObject2 = "Nazar";

                data.personDataList.Add(data.PersonData);
            }
            var xml = this.SerializeXml<PersonDatas>(data);

            return xml;

The above generated below

xml:<?xml version="1.0" encoding="utf-16"?>
<personDatas xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <personData>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <anotherObject1>ali</anotherObject1>
        <anotherObject2>Nazar</anotherObject2>
    </personData>
    <personData>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <anotherObject1>ali</anotherObject1>
        <anotherObject2>Nazar</anotherObject2>
    </personData>
    <PersonData>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <affdatalist>
            <object1>LALALALALLALA</object1>
            <object2>lqlqlqlqlqlqlql</object2>
            <object3>ililililililililililil</object3>
        </affdatalist>
        <anotherObject1>ali</anotherObject1>
        <anotherObject2>Nazar</anotherObject2>
    </PersonData>
</personDatas>

Comments

0

If you want c# classes like this

using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace namespace
{

    public class AffData {

        public string Property1 { get; set; }
    }




    public class AnotherObject1 {

        public string Property1 { get; set; }
    }


    public class AnotherObject2 {

        public string Property1 { get; set; }
    }


    public class PersonData {

        public List<AffData> AffDataList { get; set; }

        public AnotherObject1 AnotherObject1 { get; set; }

        public AnotherObject2 AnotherObject2 { get; set; }
    }

The below xml will work if you serialize List

<ArrayOfPersonData>
 <PersonData>
   <ArrayOfAffData>
     <AffData> 
<Property1>info</Property1>
 </AffData>
     <AffData> <Property1>info</Property1></AffData>
     <AffData> <Property1>info</Property1> </AffData>
   </ArrayOfAffData>
   <anotherObject1> <Property1>info</Property1> </anotherObject1>
   <anotherObject2> <Property1>info</Property1> </anotherObject2>
 </PersonData>
</ArrayOfPersonData>

1 Comment

The problem is that i dont want to have the extra elements like <AffData>.. I need to have it exactly as the structure mentioned in my question.
0
 public static string ConvertToXml(object payload, string rootElement, bool addPrefixSuffix)
    {
        string json = JsonConvert.SerializeObject(payload);
        string elementName = "DummyRoot";
        if (addPrefixSuffix)
        {
            string prefix = "{" + elementName + ":";
            string postfix = "}";
            json = string.Concat(prefix, json, postfix);
        }
        var payloadXml = JsonConvert.DeserializeXNode(json, rootElement)?.ToString();
        if (addPrefixSuffix)
        {
            payloadXml = payloadXml.ToString().Replace("<" + elementName + ">", string.Empty);
            payloadXml = payloadXml.ToString().Replace("</" + elementName + ">", string.Empty);
        }
        return Regex.Replace(payloadXml, @"\s+", String.Empty);
    }

Use the above function like ConvertToXml(requeststring, "", false)... Here second parameter empty will returns your expected result.

Comments

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.