I can create an object to hold a deserialized xml file. Mapping Xml elements to objects is easy, i just create properties in the in the class matching the name of the element. But how can i map Xml attributes to the class. For example, if i have this:
<Typestyle name="" location=开发者_如何转开发"" />
I want to deserialize the name and location attributes into properties on my class?
why not use the xsd.exe tool in the .NET framework SDK to create C# class code representing the schema. Then add those classes to your project and you can use XmlSerializer with those classes without needing to write the class code yourself.
Try this http://msdn.microsoft.com/en-us/library/x6c1kb0s.aspx
Look at XmlAttributeAttribute
class.
public class TypeStyle
{
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("location")]
public string Location{ get; set; }
}
public class Typestyle
{
[XmlAttribute]
public string name { get; set; }
[XmlAttribute]
public string location { get; set; }
}
精彩评论