XML error during deserialize - c#

I have problem when I'm trying to deserialize an XML to object. My XML look like:
<?xml version="1.0" encoding="utf-16"?>
<Products
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AllProducts>
<Product>
<ID>8</ID>
<GID>51280</GID>
<Kod>RNIKAKC1.6</Kod>
<Name>SB-800</Name>
<Ean>0018208048014</Ean>
<CommodityGroup>
<ID>86</ID>
<Name>AKCESORIA FOTO</Name>
<Path>
<Category>
<ID>60798</ID>
<Name>ARCHIWALNE</Name>
</Category>
</Path>
</CommodityGroup>
</Product>
....
Next products
...
My method code:
var MemoryStream = APIAccess.DownloadFileToStream("example.xml", "exampleContainer");
using (MemoryStream)
{
MemoryStream.Position = 0;
using (StreamReader StreamReader = new StreamReader(MemoryStream))
{
XmlSerializer serializer = new XmlSerializer(typeof(CommodityGroup));
var products = serializer.Deserialize(StreamReader);
}
}
Method DownloadFileToStream is working good, because it is useful in other classes.
I'm geting error:
InvalidOperationException: Products xmlns='' was not expected.
I want to create object of a Node CommodityGroup. I've created class selecting this node, coping it and pasting in the new class like Paste Special -> XML
Attributes of this class looks like:
[Serializable()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[XmlTypeAttribute(AnonymousType = true)]
[XmlRootAttribute(Namespace = "CommodityGroup", IsNullable = false)]
I don't know to fix it. When I'm adding into XML Serializer param new XmlRootAttribute("Products"), I'm getting "0" values.
Do you have any suggestions?

If you want to deserialize only part of an xml document, you should skip unnecessary nodes.
Do it using XmlReader.
using (StreamReader StreamReader = new StreamReader(MemoryStream))
using (var xmlReader = XmlReader.Create(StreamReader))
{
xmlReader.ReadToFollowing("CommodityGroup");
XmlSerializer serializer = new XmlSerializer(typeof(CommodityGroup));
var commodityGroup = (CommodityGroup)serializer.Deserialize(xmlReader);
}

Related

XML deserialization with or without namespace

I am trying to serialize and deserialize responses I am getting from service. The response is xml. The problem is that response can contain namespaces.
This is a response example which contains xmlns in root element and xmlns:ns2 in child:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootElement version="4.0" xmlns="http://www.test.com/test">
<Verification xmlns="" xmlns:ns2="http://www.test.com/test">
<!--some fields-->
</Verification>
</RootElement>
This is another response which does not contain xmlns and xmlns:n2:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<RootElement version="2.0">
<Verification>
<!--some fields-->
</Verification>
</RootElement>
To deserialize this response and then to serialize again I am using this code:
public static object XmlDeserializeFromString(this string objectData, Type type)
{
var serializer = new XmlSerializer(type);
object result;
using (TextReader textReader = new StringReader(objectData))
{
using (XmlTextReader xmlReader = new XmlTextReader(textReader))
{
// xmlReader.Namespaces = false;
result = serializer.Deserialize(xmlReader);
}
}
return result;
}
public static string XmlSerializeToString(this object objectInstance, System.Text.Encoding encoding)
{
var serializer = new XmlSerializer(objectInstance.GetType());
var sb = new StringBuilder();
var settings = new XmlWriterSettings
{
Encoding = encoding ?? System.Text.Encoding.UTF8,
Indent = true,
NewLineHandling = NewLineHandling.Replace
};
using (var sw = new EncodedStringWriter(encoding, sb))
{
using (var xw = XmlWriter.Create(sw, settings))
{
serializer.Serialize(xw, objectInstance);
}
}
return sb.ToString();
}
The class: This works for first type or responses but not for second. If I'll remove Namespace from attribute it will work for second but not for first type.
[Serializable, XmlRoot("RootElement", IsNullable = false, Namespace="http://www.test.com/test")]
public class RootElement
{
[XmlAttribute("version")] public string Version;
[XmlElement(Namespace="http://www.test.com/test")]
public Verification[] Verification;
}
public class Verification
{
[XmlAttribute("vendor")] public string Vendor;
}
with xmlReader.Namespaces = false line uncommented deserialization fails for first example, with commented it fails for second example. I tried to override the NamespaceURL property from XmlTextReader to ignore namespaces like it is described here and in other questions and answers and articles, also tried to override Read() method but it does not helped. I also tried to add or remove urls from class attributes but it does not helped too.
Is it possible to create a serialize and deserialize methods which can do the job for both responses? What I am doing wrong?

Deserializing XML Array in unity

Im trying to deserialize an array of objects from a XML Document.
The document built in the following structure:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<element>
.....
</element>
<element>
.....
</element>
</root>
But for some reason Im having lots of problems doing so.
This is my function which I call to deserialize it:
public static CardModel[] Load(string text)
{
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "root";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(CardModel[]),xRoot);
StringReader reader = new StringReader(text);
CardModel[] o = serializer.Deserialize(reader) as CardModel[];
reader.Close();
return o;
}
And I am not sure if its done correctly or not. Because I know that in json you are unable to deserialize an array and you have to do some sort of "hack".
In the CardModel class (which is the element of the array) i use above the class the tag [XmlRoot("root")]. I have also tried to use [XmlRoot("element")] but still im getting stuck.
Afaik you can't directly deserialize into an array but would need a wrapper class like
[Serializable]
[XMLRoot("root")]
public class Root
{
// This does the magic of treating all "element" items nested under the root
// As part of this array
[XmlArray("element")]
public CardModel[] models;
}
And rather deserilialize into that like
public static CardModel[] Load(string text)
{
// I don't think that you need the attribute overwrite here
var serializer = new XmlSerializer(typeof(Root));
using(var reader = new StringReader(text))
{
var root = (Root) serializer.Deserialize(reader);
return root.models;
}
}

Most efficient way to determine how class use to Deserialize XML

I have many .xsd files for many xml schemas
example
XML 1.0 - xml_1_0.xml
<?xml version="1.0" encoding="UTF-8"?>
<cars version="1.00">
<car>Honda</car>
<car>Ferrari</car>
</cars>
XML 2.0 - xml_2_0.xml
<?xml version="1.0" encoding="UTF-8"?>
<cars version="2.00">
<car>
<name>Honda</name>
<color>White</color>
</car>
<car>
<name>Honda</name>
<color>Red</color>
</car>
</cars>
I create my classes from .xsd like this
xsd.exe cars_1_0.xsd /c
xsd.exe cars_2_0.xsd /c
And Deserialize like this:
foreach(string file in files) {
XmlDocument doc = new XmlDocument();
doc.Load(file);
string version = doc.SelectSingleNode("/Cars/#version").Value;
if(version == "1.00")
{
Stream reader = new FileStream(file, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(v1.Cars));
v1.Cars XML = new v1.Cars();
XML = (v1.Cars)serializer.Deserialize(reader);
}
else if(version == "2.00")
{
Stream reader = new FileStream(file, FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(v2.Cars));
v2.Cars XML = new v2.Cars();
XML = (v2.Cars)serializer.Deserialize(reader);
}
}
Does anyone know a better way to do this, or have a better performance?
You have several options, depending on how far you want to take this. One fairly non invasive option would be to not use XmlDocument and avoid loading the stream more than once. For example, your existing code could be simplified/streamlined to :
foreach (string file in files)
{
using (var stream = new FileStream(file, FileMode.Open))
{
var settings = new XmlReaderSettings();
settings.CloseInput = false;
string version = "";
using (var xmlReader = XmlReader.Create(stream))
{
if (xmlReader.ReadToFollowing("Cars"))
{
version = xmlReader.GetAttribute("version");
}
else
{
throw new XmlException("Could not get 'version' attribute of 'Cars' root element!");
}
}
stream.Position = 0;
if (version == "1.00")
{
XmlSerializer serializer = new XmlSerializer(typeof(v1.Cars));
v1.Cars XML = new v1.Cars();
XML = (v1.Cars)serializer.Deserialize(stream);
}
else if (version == "2.00")
{
XmlSerializer serializer = new XmlSerializer(typeof(v2.Cars));
v2.Cars XML = new v2.Cars();
XML = (v2.Cars)serializer.Deserialize(stream);
}
}
}
Since you're just reading off the root element, you might even be able to get away with deserializing from the XmlReader and not have to reset the position on the FileStream.
This avoids the overhead of loading the entire file twice (once for XmlDocument, then again for XmlSerializer) - and particularly avoids the memory overhead of creating a DOM for each document.
A more nuclear option would be implementing IXmlSerializable on a set of custom classes, which would have custom logic in the ReadXml methods to parse the version attribute and instantiate the correct child type(s) - e.g. a CarCollection class that has a List<Car> property, where Car is an abstract class that has CarV1 and CarV2 as descendants. This would be about as efficient as you could get (and offer very fine grained control over your class hierarchy design), but would eliminate the possibility of using xsd.exe to generate your classes.

XML deserialize iso 20022 pain.001.001.03 from xsd using c#

I want to get an object from an xml file. In my example I am using iso 2002 pain.001.001.03
I have downloaded the schema from
pain.001.001.03.xsd
and the xml file from
pain.001.001.03.xml
I have validated my xml against the xsd using this tool
Validate XML
I have generated a class using xsd
and I am using the code below in order to deserialize
XmlSerializer ser = new XmlSerializer(typeof(CustomerCreditTransferInitiationV03), new XmlRootAttribute
{
ElementName = "Document",
Namespace = "urn:iso:std:iso:20022:tech:xsd:pain.001.001.03",
});
FileStream myFileStream = new FileStream("C:\\001.001.03\\pain.001.001.03.xml", FileMode.Open);
CustomerCreditTransferInitiationV03 myObject = (CustomerCreditTransferInitiationV03) ser.Deserialize(myFileStream);
The code return null values but my xml has values
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
<CstmrCdtTrfInitn>
The root element is an Document, and not a CstmrCdtTrfInitn :
var serializer = new XmlSerializer(typeof(Document));
using (var file = File.OpenRead(path))
{
var document = (Document)serializer.Deserialize(file);
var transfer = document.CstmrCdtTrfInitn;
}

XML Serializer C# Namespaces Duplicate Issue

I am using XmlSerializer in C# to generate an XML document based on a model. I need to generate the following XML root that contains a duplicate namespace using separate prefixes. Below is what the output should look likeā€¦
<ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:hl7-org:v3 CDA_SDTC.xsd"
xmlns="urn:hl7-org:v3"
xmlns:cda="urn:hl7-org:v3"
xmlns:sdtc="urn:hl7-org:sdtc">
However, when I Serialize this, the default entry is removed (which contains the duplicate namespace) and the root is prefixed.
<cda:ClinicalDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sdtc="urn:hl7-org:sdtc" xsi:schemaLocation="http://www.w3.org/2001/XMLSchema-instance" xmlns:cda="urn:hl7-org:v3">
Here is my XmlSerializer code...
var writer = new XmlSerializer(clinicalDocument.GetType(),"urn:hl7-org:v3");
var myNamespace = new XmlSerializerNamespaces();
myNamespace.Add("sdtc", "urn:hl7-org:sdtc");
myNamespace.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
myNamespace.Add("cda", "urn:hl7-org:v3");
using (var file = new System.IO.StreamWriter(CCDUncOutputPath))
{
writer.Serialize(file, clinicalDocument, myNamespace);
file.Close();
};
writer = null;
GC.Collect();
Does anyone have a fix for this?

Categories