首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何用c#建立对象图并转换成xml文件

如何用c#建立对象图并转换成xml文件
EN

Stack Overflow用户
提问于 2017-10-25 14:45:34
回答 1查看 47关注 0票数 0

附加的输出xml文件......我还需要创建一个合适的类,输出文件应该如下所示:

代码语言:javascript
复制
<CMS>
  <Device TB="CM_HOST" properties="{'Name':'Host', 'Type' : 'TR' }" >
      <PortA Connected_BY= "MiniBot">
        <Device TB="TR" ParentConnectedToPort ='PortB' properties="{'Pins': {'MiniBot_minus_pin': 2, 'MiniBot_pluse_pin': 3}, 'Type': 'TR' , 'FTDI_Port':0 ,'Name':'SV_Board','Cable': '20G Passive' }" >   
          <PortB Connected_BY= "MiniBot">
            <Device TB="AR" ParentConnectedToPort ='PortB' properties="{'Pins': {'MiniBot_minus_pin': 0, 'MiniBot_pluse_pin': 1},'Type': 'AR' , 'FTDI_Port':0 ,'Name':'StarTechDoc','Cable': '20G Passive' }">
                <PortA Connected_BY= "Directly">
                    <Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" ></Device>
                </PortA>
                <PortB Connected_BY= "ParentConnected"></PortB>
                <PortE><Device TB="None" properties="{'Type': 'None' , 'FTDI_Port':0 ,'Name':'samsung-USB3','Cable': '20G Passive'}" ></Device></PortE>
            </Device>
          </PortB>
            <PortA Connected_BY= "ParentConnected"></PortA>
          <PortE Connected_BY= "None"></PortE>
        </Device>
      </PortA>
      <PortB Connected_BY= "None"></PortB>
  </Device>
</CMS>
EN

回答 1

Stack Overflow用户

发布于 2017-10-25 15:09:38

代码语言:javascript
复制
[XmlRoot(ElementName = "Device")]
public class Device
{
    [XmlAttribute(AttributeName = "TB")]
    public string TB { get; set; }
    [XmlAttribute(AttributeName = "properties")]
    public string Properties { get; set; }
}

[XmlRoot(ElementName = "PortA")]
public class PortA
{
    [XmlElement(ElementName = "Device")]
    public Device Device { get; set; }
    [XmlAttribute(AttributeName = "Connected_BY")]
    public string Connected_BY { get; set; }
}

[XmlRoot(ElementName = "PortB")]
public class PortB
{
    [XmlAttribute(AttributeName = "Connected_BY")]
    public string Connected_BY { get; set; }
}

[XmlRoot(ElementName = "PortE")]
public class PortE
{
    [XmlElement(ElementName = "Device")]
    public Device Device { get; set; }
    [XmlAttribute(AttributeName = "Connected_BY")]
    public string Connected_BY { get; set; }
}

[XmlRoot(ElementName = "CMS")]
public class CMS
{
    [XmlElement(ElementName = "Device")]
    public Device Device { get; set; }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46925649

复制
相关文章

相似问题

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