开发者

How to deserialize XML attributes

开发者 https://www.devze.com 2023-03-29 23:32 出处:网络
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 at

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; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消