Reputation: 356
I am having an issue with understanding how best to capture the correct xml section and saving the serialized data to a db in the correct format?
Normally i will obtain a single result from an api and thus far everything has been fine in regards to serialization and deserialization: Sample xml:
<?xml version="1.0" encoding="UTF-8" ?>
<itemsApi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<itemDatas>
<itemData itemId="1233456789" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
</itemsApi>
The code to work on the single item for serialization/deserialization is as follows:
public class XmlSerializationManager<T>
{
private readonly Type _type;
public XmlSerializationManager()
{
_type = typeof(T);
}
public void Save(string path, object obj)
{
using (TextWriter textWriter = new StreamWriter(path))
{
var serializer = new XmlSerializer(_type);
serializer.Serialize(textWriter, obj);
textWriter.Close();
}
}
public T Read(string fileContent)
{
T result;
using (TextReader textReader = new StringReader(fileContent))
{
var serializer = new XmlSerializer(_type);
result = (T)serializer.Deserialize(textReader);
}
return result;
}
}
This single item deserializes and serializes just fine, however in order to check if items have been updated i present an update id that returns a list of items with all those that have received changes which maybe price or product detail such as the following example:
<?xml version="1.0" encoding="UTF-8" ?>
<itemsApi xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<itemDatas>
<itemData itemId="1233456789" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
<itemDatas>
<itemData itemId="34523452345" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
<itemDatas>
<itemData itemId="456754675467" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
<itemDatas>
<itemData itemId="2345567898907" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
<itemDatas>
<itemData itemId="098721354" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemData>
</itemDatas>
</itemsApi>
The problem I am seeing is when i work on the list as a foreach itemdatas, the schema class that is generated from an xsd causes the concatenated format for the xml node names when saving to the database:
Schema class snippet:-
/// <remarks />
[GeneratedCode("xsd", "4.6.1590.0")]
[Serializable]
[DebuggerStepThrough]
[DesignerCategory("code")]
[XmlType(AnonymousType = true)]
public class itemsApiProgramMappings
{
private itemsApiItemDatasItemData[] itemDataField;
private string typeField;
/// <remarks />
[XmlElement("itemData")]
public itemsApiItemDatasItemData[] itemData
{
get => itemDataField;
set => itemDataField = value;
}
/// <remarks />
[XmlAttribute]
public string type
{
get => typeField;
set => typeField = value;
}
}
This causes the serialized data to look like the below when saving to the db, as can be seen the list item has the node names concatenated as per the schema class however this is problematic.
<?xml version="1.0" encoding="UTF-8" ?>
<itemsApiItemDatasItemData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"itemId="1233456789" itemAdded="2020-04-26T17:13:06Z" itemUpdateId="123456789">
<node1>Sample</node1>
<node2>Sample</node2>
<node3>
<childNode1>Sample</childNode1>
<childNode2>Sample</childNode2>
</node3>
</itemsApiItemDatasItemData>
Would like to achieve is to maintain the structure of the single result format when saving to the db and currently the structure presented back from the schema class is causing me errors later on when retrieving the stored data and trying to deserialize this and i think i have really gone somewhat blind to the solution at the moment.
Do hope i have explained this well and trust someone can highlight the error of my ways as im a tad stuck in this rabbit hole.
Upvotes: 1
Views: 298
Reputation: 442
You serialize the Schema data as a whole i think it's picking up on this type name public itemsApiItemDatasItemData[] itemData
you could try doing something like this
public class TaxRates {
[XmlElement(ElementName = "TaxRate")]
public decimal ReturnTaxRate;
}
or refere to this documentation for more information
i think this one will suit your need
Upvotes: 1