如何反序列化XML内容?

时间:2017-12-26 04:18:28

标签: c# xml

我想在下面的示例中反序列化。

我可以获得大多数attritubes,但在ViewElementDetail中,我不知道如何获取它(查询)。

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

1 个答案:

答案 0 :(得分:1)

使用[XmlText]属性标记应从元素textContent反序列化的属性:

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

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