Deserializing XML, how do I access attributes?

后端 未结 1 1529
长情又很酷
长情又很酷 2021-01-13 01:04

I have some XML that I am consuming and deserializing.


    
        ...
        ...

        
相关标签:
1条回答
  • 2021-01-13 01:43

    Normally:

    [XmlAttribute]
    

    (with optional name, namespace, etc) is what you are after.

    However, you can't use that directly on a collection. You would need instead to have a wrapper class for Bars, with the attribute and a:

    public class Foo {
        public BarWrapper Bars {get;set;}
    }
    public class BarWrapper {
        private readonly List<Bar> bars = new List<Bar>();
        [XmlElement("Bar")]
        public List<Bar> Items {get{return bars;}}
    
        [XmlAttribute]
        public int Baz {get;set;}
    }
    public class Bar {...}
    
    0 讨论(0)
提交回复
热议问题