首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将实体对象转换为XML

如何将实体对象转换为XML
EN

Stack Overflow用户
提问于 2016-06-21 19:14:16
回答 1查看 315关注 0票数 0

我有一个具有许多实体(javax.persistence.Entity)对象的遗留系统。这些实体中的每一个都与其他实体具有一对多的关系。

我的要求是通过REST API公开这些实体。我计划使用resteasy (当前产品运行在jboss-7上)。我的问题是,最好的设计方法是什么?

最初,我考虑使用带JAXB注释的DTO对象,并使用getter/setter转换所有实体。还有没有别的选择?

EN

回答 1

Stack Overflow用户

发布于 2016-06-21 19:23:09

代码语言:javascript
复制
Create One class like this

    public class Employee
    {
         public int employee_code {set; get; } 
         public string first_name {set; get; } 
         public string middle_name {set; get; }
         public string last_name {set; get; }
         public string designation {set; get; }
         public string department {set; get; }
         public string present_address {set; get; }
         public string permament_address {set; get; }
         public DateTime DOB {set; get; }
         public Double Gross_Salary {set; get; }
    } 

now create a method for xml creation using this namespace
    using System.Xml.Serialization;

public string CreateXML(Object YourClassObject){    
      XmlDocument xmlDoc =new XmlDocument();   //Represents an XML document, 
                // Initializes a new instance of the XmlDocument class.          
      XmlSerializer xmlSerializer = new             XmlSerializer(YourClassObject.GetType());            
    // Creates a stream whose backing store is memory. 
       using (MemoryStream xmlStream =new MemoryStream())
       { 
        xmlSerializer.Serialize(xmlStream, YourClassObject);
        xmlStream.Position = 0;
        //Loads the XML document from the specified string.
        xmlDoc.Load(xmlStream); 
        return xmlDoc.InnerXml;
       }
}

now call this method
string strView =CreateXML(YourClassObject);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37942897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档