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" />
Related
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;
}
}
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;
}
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.
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 :-)
This is killing me. I've read these:
http://msdn.microsoft.com/en-us/library/athddy89(v=VS.80).aspx
http://msdn.microsoft.com/en-us/library/2baksw0z(v=VS.80).aspx
But I don't see how to apply them to what I'm trying to do. I want to customize the way the following list serializes...
[Serializable]
public class FinalConcentrations : List<string> { }
so that when I pass it as the "objectToSerialze" to this...
public void serializeObject(object objectToSerialize, Stream outputStream)
{
// removes the default added namespaces
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
XmlSerializer serial = new XmlSerializer(objectToSerialize.GetType());
MemoryStream ms = new MemoryStream();
serial.Serialize(ms, objectToSerialize, ns);
StreamReader reader = new StreamReader(ms);
ms.Position = 0;
ms.WriteTo(outputStream);
}
...it writes this to the output stream:
<FinalConcentrations>
<FinalConcentration>string value 1</FinalConcentration>
<FinalConcentration>string value 2</FinalConcentration>
<FinalConcentration>string value 3</FinalConcentration>
</FinalConcentration>
...instead of this
<FinalConcentrations>
<string>string value 1</string>
<string>string value 2</string>
<string>string value 3</string>
</FinalConcentration>
My serializeObject method is used to serialize a wide variety of objects, so I'm looking for a way to do this in my FinalConcentrations definition rather than within that method.
Please, help.
The easiest way to fix that is to pass in a wrapper object instead of the list itself, i.e.
public class FinalConcentrations {
private readonly List<string> items = new List<string>();
[XmlElement("FinalConcentration")]
public List<string> Items {get {return items;}}
}
that do?
Well, when I ran your example I actually got
<?xml version="1.0"?>
<ArrayOfString>
<string>Red</string>
<string>Green</string>
<string>Blue</string>
</ArrayOfString>
but by changing
[Serializable, XmlRoot( ElementName= "FinalConcentrations")]
public class FinalConcentrations : List<string> { }
I got
<?xml version="1.0"?>
<FinalConcentrations>
<string>Red</string>
<string>Green</string>
<string>Blue</string>
</FinalConcentrations>
QED?
There are a whole bunch of XML decorator attributes that can change the serialisation, eg. XmlElement. Worth having a look at.
Best of luck.