C#XML反序列化XmlAttribute [英] C# XML deserialization XmlAttribute

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

问题描述

2假设我有这个数组:

2Let's say I have this array:

<something>
    <items1 note="some text">
        <item1></item1>
        <item1></item1>
        <item1></item1>
    </items1>
    <items2>
        <item2></item2>
        <item2></item2>
        <item2></item2>
    </items2>
</something>

我有一个模型:

public class Something
{
    public string Item1Note { get; set; }
    public List<Item1> Items1 { get; set; }
    public List<Item2> Items2 { get; set; }
}

因此,是否可以将XML反序列化到模型中,以使Items1节点的属性注释位于属性Item1Note中.提前谢谢.

So, Is it possible to deserialize XML into the model so that the attribute note of Items1 node was in property Item1Note. Thx in advance.

编辑:我知道该注释是Items1的属性,但是我没有此类.

I understand that note is property of Items1, but I don't have such class.

推荐答案

该xml的类将是

public class Items1
{
    [XmlAttribute]
    public string note { get; set; }
    [XmlElement]
    public List<item1> item1 { get; set; }
}

public class Item2
{
    [XmlElement]
    public List<item2> item2 { get; set; }
}

[XmlRootAttribute("Something", Namespace="", IsNullable=false)]
public class Something
{
   [XmlElement]
    public Items1 items1 { get; set; }
    [XmlElement]
    public Item2 item2 { get; set; }
}


Something objSomething = this.Something();

ObjectXMLSerializer<Something>.Save(objSomething, FILE_NAME);

Loading the xml

objSomething = ObjectXMLSerializer<Something>.Load(FILE_NAME);

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

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