It's been awhile since i've used DataContractSerializer and i'm having a little trouble deserializing a xml file.
<?xml version="1.0" encoding="utf-8"?>
<SoftwareLicense xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
The error i'm getting is:
{"Error in line 1 position 117. Expecting element 'SoftwareLicense' from namespace 'http://schemas.datacontract.org/2004/07/Solentim.Modules.Licensing.Activation'..
Encountered 'Element' with name 'SoftwareLicense', namespace ''. "}
[DataContract(Name = "SoftwareLicense")]
public class SoftwareLicense : ISoftwareLicense
{
...
}
I've tried specifying the namespace which also doesn't work.
var serializer = new DataContractSerializer(typeof(SoftwareLicense));
using (var stream = new FileStream(filename, FileMode.Open, FileAccess.Read))
{
using (var reader =
XmlDictionaryReader.CreateTextReader(stream, new XmlDictionaryReaderQuotas()))
{
return (SoftwareLicense) serializer.ReadObject(reader);
}
}
The namespace of the file has recently changed and an interface added to the class. I've resolved all other issues i just can't seem to get around this one.
I prefer to use the DatacontractSerializer as the class now has interface properties and XMLSerializer won't deserialise it
This answer may help you to solve your problem.
If you prefer to use XmlSerializer. Here is the simple implementation below:
private T Deserialize<T>(string path) where T : class
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
T result = null;
using (XmlReader reader = XmlReader.Create(path))
{
result = (T)serializer.Deserialize(reader);
}
return result;
}
Related
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?
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.
I have a problem when I deserialize the xml into List of Objects. I searched it on the net this morning, but my problem isn't resolved.
Deserialization method
public static List<FileAction> DeSerialization()
{
XmlRootAttribute xRoot=new XmlRootAttribute();
xRoot.ElementName="ArrayOfSerializeClass";
xRoot.IsNullable=true;
XmlSerializer serializer = new XmlSerializer(typeof(List<FileAction>),xRoot);//, new XmlRootAttribute("ArrayOfSerializeClass")
using (Stream streamReader = File.OpenRead(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml"))//FileStream fs =new FileStream(xmlPath,FileMode.Open)
{
using (XmlReader reader = XmlReader.Create(streamReader))
{
int count =0;
List<FileAction> serialList2 = (List<FileAction>)serializer.Deserialize(reader);
return (List<FileAction>)serializer.Deserialize(reader);
}
}
Calling Method
String resultPath = #"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
if (!File.Exists(resultPath))
{
XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
using (FileStream fileStream = new FileStream(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Create))
{
xs.Serialize(fileStream, serializeList);//seri
fileStream.Close();
}
Console.WriteLine("Succesfully serialized to XML");
}
else
{
//string path= #"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml";
DeSerialization();
XmlSerializer xs = new XmlSerializer(typeof(List<SerializeClass>));
FileStream fs = new FileStream(#"C:\serialization\SerializationWithFileWatcher\Output\XmlSerialize.xml", FileMode.Append, FileAccess.Write);
using (XmlWriter xwr = XmlWriter.Create(fs))//TextWriter xwr = new StreamWriter
{
xs.Serialize(xwr, serializeList);//seri
//fs.Close();
}
Console.WriteLine("Succesfully serialized to XML");
}
return serializeList;
The reason why I am calling it here is that I want to add this object again to the xml file.
THe error is that here is an error in XML document (15,27).
My Xml structure
<?xml version="1.0"?>
<ArrayOfSerializeClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SerializeClass>
<creationTime>2013-11-25T09:53:25.3325289+05:30</creationTime>
<fileAction>Renamed</fileAction>
<Properties>
<FileAttributes fileName="validate json.txt">
<fileSize>307</fileSize>
<extension>.txt</extension>
<lastAccessTime>2013-11-25T09:53:25.3325289+05:30</lastAccessTime
<fullPath>C:\serialization\SerializationWithFileWatcher\SerializationWithFileWatcherProj\validate json.txt</fullPath>
</FileAttributes>
</Properties>
</SerializeClass>
</ArrayOfSerializeClass>
What I understand from the code above is that you are trying to extend the current XML, by first reading it as a FileStream and then using an XmlWriter to add some more content to it.
If my understanding is correct, then you are trying to write to the end of an existing XML file, which is not allowed since any XML document can have only one root node. In your case, that root node is ArrayOfSerializeClass.
So, in order to successfully achieve your task, you must append your XML within the root node.
Update:
Possible solution here: how to append a xml file in c#?
I have a class called School that is serializable. When it serializes/deserializes I need the root element to be called school not School without having to change the class name to school. So I used the xmlroot attribute in the following way:
[XMLRoot(ElementName = "school")]
I also tried:
[XMLRoot("school")]
Neither of these did anything and the resulting XML file contained a root element called School.
Am I missing something?
I do not see what could be the problem but the following code works with MonoTouch 4 (maybe you'll find a difference between it and your own code).
I defined a class like:
[XmlRoot ("School")]
public class Wrong {
public string Name { get; set; }
}
Then I serialized it to a MemoryStream which I then read into a string.
Wrong bad = new Wrong ();
XmlSerializer ser = new XmlSerializer(typeof(Wrong));
using (MemoryStream ms = new MemoryStream ()) {
ser.Serialize (ms, bad);
ms.Position = 0;
StreamReader sr = new StreamReader (ms);
string st = sr.ReadToEnd ();
}
The value of 'st' is:
<?xml version="1.0" encoding="utf-8"?>
<School xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />
Im trying to Serialize an object( a class in this case) with an specific fomat.
I got something like this:
<ns:pay xmlns:ns="http://example.uri.here">
<ns:Payment>
<ns:customerKeyValue>5555</ns:customerKeyValue>
<ns:bankCode>BBBB</ns:bankCode>
<ns:paymentAmount>456</ns:paymentAmount>
<ns:paymentCategory>KD</ns:paymentCategory>
<ns:paymentMode>AC</ns:paymentMode>
<ns:referenceNumber>123A</ns:referenceNumber>
<ns:userID>Test2</ns:userID>
<ns:invoiceNumber>61</ns:invoiceNumber>
</ns:Payment>
</ns:pay>
I have the class that have each element but when i serialize it it convert its to this format:
<?xml version="1.0"?>
<ns_x003A_pay xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://example.uri.here">
<CustomerKeyValue>5555</CustomerKeyValue>
<BankCode>BBBB</BankCode>
<PaymentAmount>456</PaymentAmount>
<PaymentCategory>KD</PaymentCategory>
<PaymentMode>AC</PaymentMode>
<ReferenceNumber>123A</ReferenceNumber>
<UserID>Test2</UserID>
<InvoiceNumber>61</InvoiceNumber>
</ns_x003A_pay>
So anyone can help me with that?
The method that im using to convert to xml is this:
public static string SerializeToXMLString(object ObjectToSerialize)
{
MemoryStream mem = new MemoryStream();
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectToSerialize.GetType());
ser.Serialize(mem, ObjectToSerialize);
ASCIIEncoding ascii = new ASCIIEncoding();
return ascii.GetString(mem.ToArray());
}
note:
to specify the namespace and class name i'm using this:
[XmlRootAttribute( "ns:pay", Namespace = "http://example.uri.here")]
in the class
If you haven't noted every XML element start with
Thanks for you help.
Ok guys I just found the answer here to my questions and i'm writing here to help people with this problem:
public static string SerializeToXMLString(object ObjectToSerialize)
{
//
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("ns", "http://example.uri.here");
//
//
XmlSerializer serializer = new XmlSerializer(ObjectToSerialize.GetType());
XmlWriterSettings writerSettings = new XmlWriterSettings();
writerSettings.OmitXmlDeclaration = true;
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
{
serializer.Serialize(xmlWriter, ObjectToSerialize,ns);
}
return stringWriter.ToString();
}
To solve the prefix :
I created a XmlSerializerNamespaces object and added the prefix that I wanted and the namespace.
To solve the ns:pay ns:payment
I created two classes: Payment and Pay.
In the pay class i added this:
[XmlRoot("pay", Namespace = "http://example.uri.here")]
In the Payment Class i added this:
[XmlRoot("pay")]
Pay Class has a property of type payment. That create the xml in this style:
<ns:pay>
<ns:payment
element here
</ns:payemnt>
</ns:pay>
Thank you guys. Sorry that I ask and found the question almost 30 minutes after asking.
There is also LINQtoXSD (not XmlSerializer, sure :-)