将XML转换为对象 [英] convert XML into Object

查看:305
本文介绍了将XML转换为对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好朋友,

我正在做一些Web应用程序,在该应用程序中我将以字符串形式返回xml,如下所示.

 <   siteconfigurationinfor  > ;  \ n <   id  >  11 <  /id  >  \ n <   siteid  >  DELH2I-4004-20 <  /siteid  ">> ;  \ n <  扇区   / >  \ n ><   omini    >  \ n 微型 / <     / >  \ n <  网站部门 >  \ n <   sectorid  >  31 <  /sectorid  >  \ n <  扇区名称 >  Sector1 /sectorname  <    >  \ n <  方位角   / >  \ n <   heightfromggound    >  \ n 安装类型 / <  一个ttype    / >  \ n <  倾斜   / >  \ n <  /sitesector  >  \ n <  网站部门 >  \ n <  >  32 /sectorid  <  >  Sector2 /sectorname  <    >  \ n <  方位角   / >  \ n <   heightfromggound    >  \ n 安装类型 / <  一个ttype    / >  \ n <  倾斜   / >  \ n <  /sitesector  >  \ n <  网站部门 >  \ n <  >  33 /sectorid  <  >  Sector3 /sectorname  <    >  \ n <  方位角   / >  \ n <   heightfromggound    >  \ n 安装类型 / <  一个ttype    / >  \ n <  倾斜   / >  \ n <  /sitesector  >  \ n <  /siteconfigurationinfor  >  


我想将此XML转换为自定义类型.任何人都可以在这里帮助我.

解决方案

我的操作方式如下:

 公共  class  MyObject
{
    公共 字符串 ID { get ; 设置; }
    公共 字符串 SiteID { get ; 设置; }

    公共 XElement AsXElement
    {
        获取
        {
             XElement  =  XElement(" 网站配置信息 XElement(" ,ID ),
                                            XElement(" ,SiteID ));
             返回 ;
        }
        设置
        {
            ID = .Element(" ).值;
            SiteID = .Element(" ).值;
        }
    }
   
    公共 MyObject(XElement )
    {
        .AsXElement= ;
    }
} 



当然,您可以使用内置的序列化工具,但是我更喜欢这样做(我的实际代码在XML格式错误或缺少值的情况下使用异常处理).


Hello friend,

I am doing some web application where I am getting return xml as string as follows.

<siteconfigurationinfor>\n  <id>11</id>\n  <siteid>DELH2I-4004-20</siteid>\n  <sectors />\n  <omini />\n  <micro />\n  <macro />\n  <sitesector>\n    <sectorid>31</sectorid>\n    <sectorname>Sector1</sectorname>\n    <clearance />\n    <azimuth />\n    <heightfromggound />\n    <mounttype />\n    <anttype />\n    <tilt />\n  </sitesector>\n  <sitesector>\n    <sectorid>32</sectorid>\n    <sectorname>Sector2</sectorname>\n    <clearance />\n    <azimuth />\n    <heightfromggound />\n    <mounttype />\n    <anttype />\n    <tilt />\n  </sitesector>\n  <sitesector>\n    <sectorid>33</sectorid>\n    <sectorname>Sector3</sectorname>\n    <clearance />\n    <azimuth />\n    <heightfromggound />\n    <mounttype />\n    <anttype />\n    <tilt />\n  </sitesector>\n</siteconfigurationinfor>


I want to convert this XML into custom type. Can anyone help me out here.

解决方案

The way I do it is like so:

public class MyObject
{
    public string ID { get; set; }
    public string SiteID { get; set; }

    public XElement AsXElement
    {
        get
        {
             XElement value = new XElement("siteconfigurationinfor", 
                                           new XElement("id", ID),
                                           new XElement("id", SiteID));
             return value;
        }
        set
        {
            ID = value.Element("id").Value;
            SiteID = value.Element("siteid").Value;
        }
    }
   
    public MyObject(XElement value)
    {
        this.AsXElement = value;
    }
}



Of course, you could use the built-in serialization stuff, but I prefer to do it this way (and my actual code uses exception handling in the event of malformed XML or missing values).


这篇关于将XML转换为对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆