C# XML deserialization XmlAttribute

吃可爱长大的小学妹 提交于 2019-12-13 05:04:05

问题


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>

And I have a model:

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

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.

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


回答1:


class for that xml will be

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);



回答2:


You can create your own parser and then save it as object. http://msdn.microsoft.com/en-us/library/cc189056%28v=vs.95%29.aspx



来源:https://stackoverflow.com/questions/16831317/c-sharp-xml-deserialization-xmlattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!