【C#】xmlを読み込んでオブジェクトへ変換する -デシリアライズ-
Dynamics365のビュー定義が格納されているSavedQueryエンティティ。
このエンティティからビューの定義をExcelに出力したいと思い、その中でビューの定義情報を格納するfetchxmlとlayoutxmlをオブジェクト化する方法を調べたのでメモ。
今回はXmlSerializerクラスのDeserializeメソッドを使用しました。
https://msdn.microsoft.com/ja-jp/library/tz8csy73%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
読み込んだXMLを格納するオブジェクト
[System.Xml.Serialization.XmlRoot("grid")] public class ViewLayoutXmlInfo { [System.Xml.Serialization.XmlArray("row")] [System.Xml.Serialization.XmlArrayItem("cell")] public List<ViewLayoutXmlCell> Cells { get; set; } public class ViewLayoutXmlCell { [System.Xml.Serialization.XmlAttribute("name")] public string Name { get; set; } [System.Xml.Serialization.XmlAttribute("width")] public string Width { get; set; } [System.Xml.Serialization.XmlAttribute("imageproviderwebresource")] public string ImageProviderWebresource { get; set; } [System.Xml.Serialization.XmlAttribute("imageproviderfunctionname")] public string ImageProviderFunctionname { get; set; } } }
読み込むXMLのサンプル(実際はSavedQueryエンティティを検索して取得します)
<grid name="resultset" object="1" jump="name" select="1" icon="1" preview="1"> <row name="result" id="accountid"> <cell name="name" width="300" /> <cell name="primarycontactid" width="150" /> <cell name="telephone1" width="100" /> </row> </grid>
// LayoutXmlの読み込み var xmlReader = XmlUtil.CreateXmlReader(layoutXml); XmlSerializer serializer = new XmlSerializer(typeof(ViewLayoutXmlInfo)); ViewLayoutXmlInfo layoutXmlRowInfo = (ViewLayoutXmlInfo)serializer.Deserialize(xmlReader);