jmooney5115
New member
- Joined
- Oct 16, 2018
- Messages
- 4
- Programming Experience
- 5-10
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.
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?
Wrapper object with only the property Description.
XML
Plugged the above code into this site to generate XML: https://xmltocsharp.azurewebsites.net/
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?
Wrapper object with only the property Description.
C#:
public class ObjectWrapper{ public string Description
{
get
{
var result = "";
try
{
if (obj is null) return result;
var columns = obj.Setting.Where(q => q.Name == "Description").ToList();
if (columns.Count == 1)
result = columns.Single().Value.ToString();
}
catch (Exception ex)
{
Debug.WriteLine("Failed to parse Description obj. Ex: " + ex.ToString());
}
return result;
}
set
{
if (string.IsNullOrEmpty(value)) return;
try
{
if (obj is null) return;
var columns = obj.Setting.Where(q => q.Name == "Description").ToList();
if (columns.Count == 1)
columns.Single().Value = value;
else
{
//if Name doesn't exist, create it and assign it.
var Name = new Setting
{
Name = "Description",
Value = value
};
obj.Setting.Add(Name);
}
}
catch (Exception ex)
{
Debug.WriteLine("Failed to parse Description obj. Ex: " + ex.ToString());
}
}
}
}
XML
C#:
<Object>
<Processor>
<Pages>
<Page>
<Name>Settings</Name>
<Row>
<Setting>
<Name>Enable</Name>
<Value>True</Value>
</Setting>
<Setting>
<Name>Name</Name>
<Value>value</Value>
</Setting>
<Setting>
<Name>Description</Name>
<Value>value</Value>
</Setting>
</Row>
<Row>
<Setting>
<Name>Enable</Name>
<Value>True</Value>
</Setting>
<Setting>
<Name>Name</Name>
<Value>value</Value>
</Setting>
<Setting>
<Name>Description</Name>
<Value>value</Value>
</Setting>
</Row>
</Page>
</Pages>
</Processor>
</Object>
Plugged the above code into this site to generate XML: https://xmltocsharp.azurewebsites.net/
C#:
/* Licensed under the Apache License, Version 2.0
[URL]http://www.apache.org/licenses/LICENSE-2.0[/URL]
*/
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="Setting")]
public class Setting {
[XmlElement(ElementName="Name")]
public string Name { get; set; }
[XmlElement(ElementName="Value")]
public string Value { get; set; }
}
[XmlRoot(ElementName="Row")]
public class Row {
[XmlElement(ElementName="Setting")]
public List<Setting> Setting { get; set; }
}
[XmlRoot(ElementName="Page")]
public class Page {
[XmlElement(ElementName="Name")]
public string Name { get; set; }
[XmlElement(ElementName="Row")]
public List<Row> Row { get; set; }
}
[XmlRoot(ElementName="Pages")]
public class Pages {
[XmlElement(ElementName="Page")]
public Page Page { get; set; }
}
[XmlRoot(ElementName="Processor")]
public class Processor {
[XmlElement(ElementName="Pages")]
public Pages Pages { get; set; }
}
[XmlRoot(ElementName="Object")]
public class Object {
[XmlElement(ElementName="Processor")]
public Processor Processor { get; set; }
}
}
Last edited: