C#系列化/反系列化XML

hbjlzjgdlgz 2016-04-04 08:03:38

各位大神们
系列化/反系列化以下的XML:
<?xml version="1.0" encoding="gb2312"?>
<StoreData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<StoreItem>
<Store Units="Yuan" Price="198.58">BookName1</Store>
<Quantity>5</Quantity>
</StoreItem>

<StoreItem>
<Store Units="Yuan" Price="39.12">BookName2</Store>
<Quantity>3</Quantity>
</StoreItem>

<StoreItem>
<Store Units="Yuan" Price="97.74">BookName4</Store>
<Quantity>9</Quantity>
</StoreItem>
</StoreData>


C#中的类该如何定义?




...全文
175 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
http://blog.csdn.net/starfd/article/details/48686239 直接用vs生成类,长的吓死人
    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class StoreData
    {

        private StoreDataStoreItem[] storeItemField;

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

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

        private StoreDataStoreItemStore storeField;

        private byte quantityField;

        /// <remarks/>
        public StoreDataStoreItemStore Store
        {
            get
            {
                return this.storeField;
            }
            set
            {
                this.storeField = value;
            }
        }

        /// <remarks/>
        public byte Quantity
        {
            get
            {
                return this.quantityField;
            }
            set
            {
                this.quantityField = value;
            }
        }
    }

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

        private string unitsField;

        private decimal priceField;

        private string valueField;

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

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal Price
        {
            get
            {
                return this.priceField;
            }
            set
            {
                this.priceField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Value
        {
            get
            {
                return this.valueField;
            }
            set
            {
                this.valueField = value;
            }
        }
    }
hbjlzjgdlgz 2016-04-05
  • 打赏
  • 举报
回复
Hi guwei4307, 我看了你给这个文章。 在根节点CATS 我希望的是在根节点StoreData下直接是很多个StoreItem。不想在根节点和StoreItem之间多出一个节点。
hbjlzjgdlgz 2016-04-05
  • 打赏
  • 举报
回复
Hi guwei4307, 我看了你给这个文章。 在根节点cats和子节点item之间也有一个节点items。 我希望的是在根节点StoreData下直接是很多个StoreItem。不想在根节点和StoreItem之间多出一个节点。
hbjlzjgdlgz 2016-04-05
  • 打赏
  • 举报
回复
Hi xdashewan,
引用 3 楼 xdashewan 的回复:
照这样看,你直接序列化public List<StoreItem> StoreData不就好了?
按照你的定义,会出现:成员名称不能与它们的封闭类型相同。
全栈极简 2016-04-05
  • 打赏
  • 举报
回复
xdashewan 2016-04-05
  • 打赏
  • 举报
回复
照这样看,你直接序列化public List<StoreItem> StoreData不就好了?
hbjlzjgdlgz 2016-04-05
  • 打赏
  • 举报
回复
Hi rocmemory, 这是我定义的类, public class StoreData { public List<StoreItem> StoreItems = new List<StoreItem>(); public class StoreItem { public store Stroe; public string Quantity; } } public class store { [XmlAttribute("Units")] public string Units; [XmlAttribute("Price")] public string Price; [XmlText] public string Value; } 但是得到的XML如下: <?xml version="1.0" encoding="gb2312"?> <StoreData> <StoreItems> <StoreItem> <Stroe Units="Yuan" Price="9.34">BookName0</Stroe> <Quantity>2</Quantity> </StoreItem> <StoreItem> <Stroe Units="Yuan" Price="9.46">BookName1</Stroe> <Quantity>3</Quantity> </StoreItem> <StoreItem> <Stroe Units="Yuan" Price="9.58">BookName2</Stroe> <Quantity>4</Quantity> </StoreItem> </StoreItems> </StoreData> 但是我希望的是在根节点StoreData下直接是很多个StoreItem。不想在根节点和StoreData之间多出一个节点。
为轮子而生 2016-04-05
  • 打赏
  • 举报
回复
你需要先定义Store这个类,然后再定义StoreItem。BookName1属性是Store的一个实例。

110,531

社区成员

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

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

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