Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XmlAttribute的XmlElement列表中序列化';s_C# - Fatal编程技术网

C# 在XmlAttribute的XmlElement列表中序列化';s

C# 在XmlAttribute的XmlElement列表中序列化';s,c#,C#,我想序列化XmlElement中的XmlAttribute列表 有关该计划的一些一般信息: 这是一个程序,您可以添加产品的,其中包含一些属性,如名称、描述、和一些属性(高度、宽度、品牌、条件),但用户可以选择要添加的数量(不是固定的数字) 以下是该程序的类图: 以下是ManagerRoot类的代码: 以下是ProductRoot类的代码: 现在我不知道如何处理类中列表的getter和setter。如何添加一个新的产品,其中包含名称,说明,链接,填充属性列表?对于集合,您需要使用XmlArray

我想序列化XmlElement中的XmlAttribute列表

有关该计划的一些一般信息: 这是一个程序,您可以添加
产品的
,其中包含一些属性,如
名称
描述
、和一些属性(高度、宽度、品牌、条件),但用户可以选择要添加的数量(不是固定的数字)

以下是该程序的类图:

以下是
ManagerRoot类的代码
: 以下是
ProductRoot类的代码:

现在我不知道如何处理类中列表的
getter
setter
。如何添加一个新的
产品
,其中包含
名称
说明
链接
填充属性列表

对于集合,您需要使用XmlArray和XmlArrayItem注释属性,以定义所包含的项。对于所有基于集合的属性,请遵循相同的ProductList模式

命名空间控制台应用程序1 {

[可序列化]
公共类ProductRoot
{
[XmlArray]
[XmlArrayItem(ElementName=“Product”,Type=typeof(Product))]
公共列表ProductList{get;set;}
}
[可序列化]
公共类产品
{
[XmlIgnore]
私有字符串_头;
[XmlAttribute(“标头”)]
公共字符串头{
获取{返回此。_头;}
设置{this.\u header=value;}
}
[XmlIgnore]
私有字符串描述;
[XmlAttribute(“说明”)]
公共字符串描述{
获取{返回此。_description;}
设置{this.\u description=value;}
}
[XmlIgnore]
私有字符串链接;
[XmlAttribute(“链接”)]
公共字符串链接{
获取{返回此。_link;}
设置{this.\u link=value;}
}
[XmlIgnore]
私人物业清单;;
[XmlAttribute(“属性”)]
公共列表属性{get;set;}
}
静态类程序
{
静态void Main(){
var productRoot=new productRoot();
var products=新列表();
products.Add(newproduct(){Description=“A”,Properties=newlist{“PropA”});
products.Add(新产品(){Description=“B”,Properties=newlist{“PropB”});
productRoot.ProductList=产品;
测试(productRoot);
}
静态孔隙试验(T obj){
使用(MemoryStream stream=new MemoryStream()){
XmlSerializer s=新的XmlSerializer(typeof(T));
使用(var stringWriter=new stringWriter()){
使用(var writer=XmlWriter.Create(stringWriter)){
s、 序列化(stringWriter,obj);
}
Console.WriteLine(stringWriter.ToString());
}
}
}
}

对于集合,您需要使用XmlArray和XmlArrayItem对属性进行注释,以定义包含的项。对于所有基于集合的属性,请遵循相同的ProductList模式

命名空间控制台应用程序1 {

[可序列化]
公共类ProductRoot
{
[XmlArray]
[XmlArrayItem(ElementName=“Product”,Type=typeof(Product))]
公共列表ProductList{get;set;}
}
[可序列化]
公共类产品
{
[XmlIgnore]
私有字符串_头;
[XmlAttribute(“标头”)]
公共字符串头{
获取{返回此。_头;}
设置{this.\u header=value;}
}
[XmlIgnore]
私有字符串描述;
[XmlAttribute(“说明”)]
公共字符串描述{
获取{返回此。_description;}
设置{this.\u description=value;}
}
[XmlIgnore]
私有字符串链接;
[XmlAttribute(“链接”)]
公共字符串链接{
获取{返回此。_link;}
设置{this.\u link=value;}
}
[XmlIgnore]
私人物业清单;;
[XmlAttribute(“属性”)]
公共列表属性{get;set;}
}
静态类程序
{
静态void Main(){
var productRoot=new productRoot();
var products=新列表();
products.Add(newproduct(){Description=“A”,Properties=newlist{“PropA”});
products.Add(新产品(){Description=“B”,Properties=newlist{“PropB”});
productRoot.ProductList=产品;
测试(productRoot);
}
静态孔隙试验(T obj){
使用(MemoryStream stream=new MemoryStream()){
XmlSerializer s=新的XmlSerializer(typeof(T));
使用(var stringWriter=new stringWriter()){
使用(var writer=XmlWriter.Create(stringWriter)){
s、 序列化(stringWriter,obj);
}
Console.WriteLine(stringWriter.ToString());
}
}
}
}
你说的“添加新产品”是什么意思?添加到哪里?为什么不实例化一个新产品对象,设置属性并添加ProductRoot对象的ProductList。这就是你要问的吗?对不起,我不清楚你的问题到底是什么?你说的“添加新产品”是什么意思?添加到何处?为什么不实例化一个新产品对象,设置属性并添加ProductRoot对象的ProductList。这就是您要问的吗?对不起,我不清楚您的问题到底是什么
[Serializable]
public class ManagerRoot
{
    [XmlElement("ProductRoot")]
    public ProductRoot ProductRoot = new ProductRoot();
}
[Serializable]
public class ProductRoot
{
    [XmlElement("Product")]
    public List<Product> ProductList { get; set; }

    private void addProduct()
    {
        //?
    }
}
[Serializable]
public class Product
{
    [XmlIgnore]
    private string _header;
    [XmlAttribute("Header")]
    public string Header
    {
        get { return this._header; }
        set { this._header = value; }
    }

    [XmlIgnore]
    private string _description;
    [XmlAttribute("Description")]
    public string Description
    {
        get { return this._description; }
        set { this._description = value; }
    }

    [XmlIgnore]
    private string _link;
    [XmlAttribute("Link")]
    public string Link
    {
        get { return this._link; }
        set { this._link = value; }
    }

    [XmlIgnore]
    private List<string> _properties;
    [XmlAttribute("Properties")]
    public List<string> Properties
    {
        get
        {

        }
        set
        {

        }
    }

}
[Serializable]
public class ProductRoot
{
    [XmlArray]
    [XmlArrayItem(ElementName = "Product", Type = typeof(Product))]
    public List<Product> ProductList { get; set; }
}

[Serializable]
public class Product
{
    [XmlIgnore]
    private string _header;
    [XmlAttribute("Header")]
    public string Header {
        get { return this._header; }
        set { this._header = value; }
    }

    [XmlIgnore]
    private string _description;
    [XmlAttribute("Description")]
    public string Description {
        get { return this._description; }
        set { this._description = value; }
    }

    [XmlIgnore]
    private string _link;
    [XmlAttribute("Link")]
    public string Link {
        get { return this._link; }
        set { this._link = value; }
    }

    [XmlIgnore]
    private List<string> _properties;
    [XmlAttribute("Properties")]
    public List<string> Properties { get; set; }
}

static class Program
{

    static void Main() {
        var productRoot = new ProductRoot();
        var products = new List<Product>();

        products.Add(new Product() { Description = "A", Properties = new List<string> { "PropA" } });
        products.Add(new Product() { Description = "B", Properties = new List<string> { "PropB" } });
        productRoot.ProductList = products;

        Test(productRoot);
    }

    static void Test<T>(T obj) {
        using (MemoryStream stream = new MemoryStream()) {
            XmlSerializer s = new XmlSerializer(typeof(T));
            using (var stringWriter = new StringWriter()) {
                using (var writer = XmlWriter.Create(stringWriter)) {
                    s.Serialize(stringWriter, obj);
                }

                Console.WriteLine(stringWriter.ToString());
            }
        }
    }
}