首页
论坛
新职位
搜索论坛
什么's new
新职位
新的个人资料帖子
最新活动
会员
目前的访客
新的个人资料帖子
搜索个人资料帖子
VB.NET社区
登录
登记
什么's new
搜索
搜索
仅搜索标题
经过:
新职位
搜索论坛
Menu
Log in
Register
安装应用
安装
首页
论坛
数据库
XML格式
将XML反序列化为对象-主要属性名称在XML元素中
您正在使用过期的浏览器。它可能无法正确显示此网站或其他网站。
您应该升级或使用
替代浏览器
.
回复主题
信息
<blockquote data-quote="jmooney5115" data-source="post: 9547" data-attributes="member: 11404"><p>Hi. I am having an issue figuring out the best way to deserialize the below XML into an easily usable object. Using a C# to XML converter I have a usable object in which I can serialize and deserialize objects. The issue is that using this XML I have to use LINQ to pull out property values. The main property names I am after are stored in an XML element called Name. Deserializing the XML will give me a list of Settings. The setting contains 2 properties, name & value; LINQ is the only way I know to get the values.</p><p></p><p>I have a wrapper class that takes the deserialized object and has a property for each of the 'Setting' property in the XML. The getter/setter for properties in the wrapper contains the LINQ required. There has to be a better way to do this right?</p><p></p><p>Wrapper object with only the property Description.</p><p></p><p>[CODE]public class ObjectWrapper{ public string Description</p><p> {</p><p> get</p><p> {</p><p> var result = "";</p><p></p><p> try</p><p> {</p><p> if (obj is null) return result;</p><p></p><p> var columns = obj.Setting.Where(q => q.Name == "Description").ToList();</p><p></p><p> if (columns.Count == 1)</p><p> result = columns.Single().Value.ToString();</p><p> }</p><p> catch (Exception ex)</p><p> {</p><p> Debug.WriteLine("Failed to parse Description obj. Ex: " + ex.ToString());</p><p> }</p><p></p><p> return result;</p><p> }</p><p> set</p><p> {</p><p> if (string.IsNullOrEmpty(value)) return;</p><p></p><p> try</p><p> {</p><p> if (obj is null) return;</p><p></p><p> var columns = obj.Setting.Where(q => q.Name == "Description").ToList();</p><p></p><p> if (columns.Count == 1)</p><p> columns.Single().Value = value;</p><p> else</p><p> {</p><p> //if Name doesn't exist, create it and assign it.</p><p> var Name = new Setting</p><p> {</p><p> Name = "Description",</p><p> Value = value</p><p> };</p><p></p><p> obj.Setting.Add(Name);</p><p> }</p><p> }</p><p> catch (Exception ex)</p><p> {</p><p> Debug.WriteLine("Failed to parse Description obj. Ex: " + ex.ToString());</p><p> }</p><p> }</p><p> }</p><p>}</p><p>[/CODE]</p><p></p><p></p><p>XML</p><p></p><p>[CODE]<Object></p><p> <Processor></p><p> <Pages></p><p> <Page></p><p> <Name>Settings</Name></p><p> <Row></p><p> <Setting></p><p> <Name>Enable</Name></p><p> <Value>True</Value></p><p> </Setting></p><p> <Setting></p><p> <Name>Name</Name></p><p> <Value>value</Value></p><p> </Setting></p><p> <Setting></p><p> <Name>Description</Name></p><p> <Value>value</Value></p><p> </Setting></p><p> </Row></p><p> <Row></p><p> <Setting></p><p> <Name>Enable</Name></p><p> <Value>True</Value></p><p> </Setting></p><p> <Setting></p><p> <Name>Name</Name></p><p> <Value>value</Value></p><p> </Setting></p><p> <Setting></p><p> <Name>Description</Name></p><p> <Value>value</Value></p><p> </Setting></p><p> </Row></p><p> </Page></p><p> </Pages></p><p> </Processor></p><p></Object></p><p>[/CODE]</p><p></p><p></p><p></p><p></p><p>Plugged the above code into this site to generate XML: <a href="//xmltocsharp.azurewebsites.net/">//xmltocsharp.azurewebsites.net/</a></p><p></p><p></p><p>[CODE]</p><p>/* Licensed under the Apache License, Version 2.0</p><p> </p><p>[URL]http://www.apache.org/licenses/LICENSE-2.0[/URL]</p><p>*/</p><p>using System;</p><p>using System.Xml.Serialization;</p><p>using System.Collections.Generic;</p><p>namespace Xml2CSharp</p><p>{</p><p> [XmlRoot(ElementName="Setting")]</p><p> public class Setting {</p><p> [XmlElement(ElementName="Name")]</p><p> public string Name { get; set; }</p><p> [XmlElement(ElementName="Value")]</p><p> public string Value { get; set; }</p><p> }</p><p></p><p> [XmlRoot(ElementName="Row")]</p><p> public class Row {</p><p> [XmlElement(ElementName="Setting")]</p><p> public List<Setting> Setting { get; set; }</p><p> }</p><p></p><p> [XmlRoot(ElementName="Page")]</p><p> public class Page {</p><p> [XmlElement(ElementName="Name")]</p><p> public string Name { get; set; }</p><p> [XmlElement(ElementName="Row")]</p><p> public List<Row> Row { get; set; }</p><p> }</p><p></p><p> [XmlRoot(ElementName="Pages")]</p><p> public class Pages {</p><p> [XmlElement(ElementName="Page")]</p><p> public Page Page { get; set; }</p><p> }</p><p></p><p> [XmlRoot(ElementName="Processor")]</p><p> public class Processor {</p><p> [XmlElement(ElementName="Pages")]</p><p> public Pages Pages { get; set; }</p><p> }</p><p></p><p> [XmlRoot(ElementName="Object")]</p><p> public class Object {</p><p> [XmlElement(ElementName="Processor")]</p><p> public Processor Processor { get; set; }</p><p> }</p><p>}</p><p>[/CODE]</p></blockquote><p></p>
Insert quotes…
确认
发表回复
首页
论坛
数据库
XML格式
将XML反序列化为对象-主要属性名称在XML元素中
本网站使用Cookie来帮助个性化内容,调整您的体验并在注册时保持登录状态。
继续使用本网站,即表示您同意我们使用cookie。
接受
了解更多…
最佳
底部