简体   繁体   中英

converting a list data to xml format using c# mvc

I get my data retrieved in this way

var parentData = dataContext.Fetch<MakerCheckerViewModel>  
(PetaPoco.Sql.Builder.Append  
  ("SELECT MakerCheckerId  
  ,ModelName  
  ,mkCk.CheckerStatusId  
  ,chkSt.CheckerStatusName  
  FROM MakerChecker as mkCk  
  JOIN CheckerStatus as chkSt ON  
mkCk.CheckerStatusId=chkSt.CheckerStatusId")  
).ToList();  

I need to convert it into json. I referred these links How to convert JSON to XML or XML to JSON? but using this it can convert only the string data but not the list types.
Also I cannot do it inside Linq as done here convert list to xml in c# as I have the bunch data which will be increasing more in future so I need something like this

    var parentDataJson = JsonConvert.SerializeObject(parentData);

which is done to convert to json. So similar functionality would be there for xml data too. Please help!!

This will return a string of XML.

public string CreateXml(MyObject myObject)
{
    var xmlDoc = new XmlDocument();

    var xmlSerializer = new XmlSerializer(myObject.GetType());

    using (var xmlStream = new MemoryStream())
    {
        xmlSerializer.Serialize(xmlStream, myObject);
        xmlStream.Position = 0;
        xmlDoc.Load(xmlStream);
        return xmlDoc.InnerXml;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM