sugy21
sugy21

Reputation: 119

How to deserialize XML contents?

I'd like to deserialize below sample.

I could get most attritubes, but inside of ViewElementDetail, I don't know how to get it (Query).

enter image description here

            using (var stream = new FileStream(file, FileMode.Open))
            {
                var serializer = new System.Xml.Serialization.XmlSerializer(typeof(List<ViewElement>));
                var aa = (List<ViewElement>)serializer.Deserialize(stream);
            }

            public class ViewElement
            {
                [XmlAttribute]
                public string ViewName { get; set; }
                [XmlAttribute]
                public string ColumnName { get; set; }
                [XmlAttribute]
                public string Description { get; set; }
                [XmlElement]
                public List<ViewElementDetail> ViewElementDetail { get; set; }
            }

            public class ViewElementDetail
            {
                [XmlAttribute]
                public string Type { get; set; }
                /// <summary>I don't know how to get this value</summary>
                [XmlAttribute]
                public string Query { get; set; }
            }

Upvotes: 0

Views: 42

Answers (1)

Dai
Dai

Reputation: 155588

Use the [XmlText] attribute to mark a property should be deserialized from an element's textContent:

class ViewElementDetial {
    [XmlAttribute]
    public string Type { get; set; }

    [XmlText]
    public String Query { get; set; }
}

Upvotes: 1

Related Questions