带有属性列表的XML序列化 [英] XML serialization of a list with attributes

查看:430
本文介绍了带有属性列表的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在另一个列表(变型产品)的列表。我想父列表会对它(只是一个 ID 名称)设置的属性。



所需的输出



<预类=郎咸平的XML prettyprint-覆盖> <&装饰GT;
<类型ID =1NAME =吧吧富>
<排>
< ID> 1 LT; / ID>
<名称>富巴≤; /名称>
<成本大于10< /成本>
< /行>
< /类型>
< /装饰>






当前代码



<预类=郎-CS prettyprint-覆盖> [XmlRoot(的ElementName =装饰,ISNULLABLE = FALSE)]
公共类EmbellishmentGroup
{
[XmlArray(的ElementName =类型)]
[XmlArrayItem(行,类型= typeof运算(产品))]
公开名单<产品>清单{搞定;组; }

公共EmbellishmentGroup(){
名单=新名单<产品及GT;();
List.Add(新产品(){ID = 1,名称=富巴,成本=10米});
}
}

公共类产品
{
[的XmlElement(ID)]
公众诠释标识{搞定;组; }

[的XmlElement(名称)]
公共字符串名称{;组; }

[XmlElement的(成本)]
公共小数成本{搞定;组; }
}






当前输出



<预类=郎咸平的XML prettyprint-覆盖> <&装饰GT;
<&型GT;
<排>
< ID> 1 LT; / ID>
<名称>富巴≤; /名称>
<成本大于10< /成本>
< /行>
< /类型>
< /装饰>





解决方案

你需要让另一个类代表了键入元素。然后,你可以将它添加属性的属性,如:

  [XmlRoot(的ElementName =装饰,ISNULLABLE = FALSE )
公共类EmbellishmentGroup
{
[XmlElement的(类型)]
公众的MyType类型{搞定;组; }

公共EmbellishmentGroup()
{
类型=新的MyType();
}
}

公共类的MyType
{
[XmlAttribute(ID)]
公众诠释标识{搞定;组; }

[XmlAttribute(名称)]
公共字符串名称{;组; }

[的XmlElement(行)]
公开名单<产品与GT;清单{搞定;组; }

公众的MyType()
{
ID = 1;
NAME =吧吧富;
名单=新名单<产品及GT;();
产品P =新产品();
p.Id = 1;
p.Name =富巴;
p.Cost =10米;
List.Add(对);
}
}

公共类产品
{
[的XmlElement(ID)]
公众诠释标识{搞定;组; }

[的XmlElement(名称)]
公共字符串名称{;组; }

[XmlElement的(成本)]
公共小数成本{搞定;组; }
}


I have a list inside another list (a product with variants). I would like the parent list to have attributes set on it (just an id and a name).

Desired Output

<embellishments>
    <type id="1" name="bar bar foo">
        <row>
            <id>1</id>
            <name>foo bar</name>
            <cost>10</cost>
        </row>      
    </type> 
</embellishments>


Current Code

[XmlRoot( ElementName = "embellishments", IsNullable = false )]
public class EmbellishmentGroup
{
    [XmlArray(ElementName="type")]
    [XmlArrayItem("row", Type=typeof(Product))]
    public List<Product> List { get; set; }

    public EmbellishmentGroup() {
        List = new List<Product>();
        List.Add( new Product() { Id = 1, Name = "foo bar", Cost = 10m } );
    }
}

public class Product
{
    [XmlElement( "id" )]
    public int Id { get; set; }

    [XmlElement( "name" )]
    public string Name { get; set; }

    [XmlElement( "cost" )]
    public decimal Cost { get; set; }
}


Current Output

<embellishments>
    <type>
        <row>
            <id>1</id>
            <name>foo bar</name>
            <cost>10</cost>
        </row>
    </type>
</embellishments>


解决方案

You need to make another class which represents the type element. Then you can add properties to it for the attributes, like this:

[XmlRoot(ElementName = "embellishments", IsNullable = false)]
public class EmbellishmentGroup
{
    [XmlElement("type")]
    public MyType Type { get; set; }

    public EmbellishmentGroup() 
    {
        Type = new MyType();
    }
}

public class MyType
{
    [XmlAttribute("id")]
    public int Id { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }

    [XmlElement("row")]
    public List<Product> List { get; set; }

    public MyType()
    {
        Id = 1;
        Name = "bar bar foo";
        List = new List<Product>();
        Product p = new Product();
        p.Id = 1;
        p.Name = "foo bar";
        p.Cost = 10m;
        List.Add(p);
    }
}

public class Product
{
    [XmlElement( "id" )]
    public int Id { get; set; }

    [XmlElement( "name" )]
    public string Name { get; set; }

    [XmlElement( "cost" )]
    public decimal Cost { get; set; }
}

这篇关于带有属性列表的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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