简体   繁体   中英

C# XML deserialization XmlAttribute

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM