C# 对象转xml xml格式比较特殊

辉警长 2018-01-04 10:12:12
xml格式比较特殊:xml节点都为Item, xml 属性对应类属性, xml值对应对像的值

对像序列化后不是我想要的格式啊,或是方法不对?如图:



常用xml格式是这样的:
<dept_id></dept_id>
<personnel_id></personnel_id>
<cust_id></cust_id>
或是带属性的:
<dept_id id="2222"/>

大家有这样的算法吗?除了使用XmlDocument 重新拼写~~~~~呵呵k
...全文
335 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
辉警长 2018-01-05
  • 打赏
  • 举报
回复
我试试,可能是对对象与xml的关系理解的不深
神马都能聊 2018-01-04
  • 打赏
  • 举报
回复
你得去理解数据的结构为什么和你的不一样,他外面多一层,说明他外面还有个对象,于是,你可以定义一个类A,这个类里包含一个List<……Acconting> Item,然后序列化A就出现这个结构了。 于是,衍生的设计就是多了一个类A,A有个List<……Acconting> Item成员,有个XML序列化方法,序列化方法的结果就是你要的结果数据。
  • 打赏
  • 举报
回复

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Items
{

    private ItemsItem[] itemField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("item")]
    public ItemsItem[] item
    {
        get
        {
            return this.itemField;
        }
        set
        {
            this.itemField = value;
        }
    }
}

/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ItemsItem
{

    private string nameField;

    private byte valueField;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string name
    {
        get
        {
            return this.nameField;
        }
        set
        {
            this.nameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlTextAttribute()]
    public byte Value
    {
        get
        {
            return this.valueField;
        }
        set
        {
            this.valueField = value;
        }
    }
}

选择性粘贴下就行了

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧