问题
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