I want to load a XML file with XML serialization. The type now should be an enum type.
So the XML looks like this:
<Ressource name="ressource_name" type= "Integer" >
...
</Ressource>
And I wanted to load it into a class like this:
[Serializable]
public enum Res_Type
{
[XmlEnum(Name = "Integer")]
Integer,
[XmlEnum(Name = "Decimal")]
Decimal,
[XmlEnum(Name = "Text")]
Text
}
public class Ressource
{
[XmlAttribute]
public string name { set; get; }
[XmlAttribute]
public Res_Type type { get; set; }
}
When I search for this topic I only find different ways of solving it, then I need it to. I need to have the XML like shown above, but I have no idea how to load the information in type as an enum.
Update:
To test the serialization and the deserialization I am using this code:
Ressource res = new Ressource();
res.name = "ressource_name";
res.type = Res_Type.Integer;
XmlSerializer serializer = new XmlSerializer(res.GetType());
using (StreamWriter writer = new StreamWriter(#"h:\test.xml"))
{
serializer.Serialize(writer, res);
}
XmlSerializer xmlSerializer = new XmlSerializer(typeof(Ressource));
StringReader stringReader = new StringReader(#"h:\test.xml");
res = (Ressource)xmlSerializer.Deserialize(stringReader);
And I am getting the error: InvalidOperationException
Your problem is that you are using a StringReader rather than a StreamReader:
StringReader stringReader = new StringReader(#"h:\test.xml");
This means that your code is attempting to deserialize the contents of the string literal #"h:\test.xml" itself rather than the file to which it refers. This of course fails because the string h:\test.xml is not even well-formed XML.
Instead you should do:
var fileName = #"h:\test.xml";
// Write the file as before
using (var reader = new StreamReader(fileName))
{
res = (Ressource)xmlSerializer.Deserialize(reader);
}
Working .Net fiddle here.
Related
I've got an XSD which has been supplied by a third party and I've then converted this into classes using xsd.exe.
Due to the fact we do not own this XSD, I'm trying to not alter the generated classes in any way.
Without manually applying a blank string check to every single field we are mapping, e.g.:
!String.IsNullOrWhiteSpace(field) ? field : null;
I can't think of an easy way to serialise the blank strings in a way which means they are not output at all (no tag is produced).
The reason we cannot output blank strings is because the XSD has pattern constraints such as StringM50m1 which I believe means a minimum length of 1 is required (however, they are happy to not receive the tag if there is no content) which fail post-output validation with the following message:
The 'field' element is invalid - The value '' is invalid according to its datatype 'StringM50m1' - The Pattern constraint failed.
Any thoughts would be appreciated.
If you use XmlSerializer you can add XmlDefaultValue for required properties:
private static void Serialize()
{
XsdClass xc = new XsdClass();
xc.SomeString1 = string.Empty;
xc.SomeString2 = "value";
xc.SomeString3 = null;
XmlAttributeOverrides overrides = new XmlAttributeOverrides();
XmlAttributes attribs = new XmlAttributes();
attribs.XmlDefaultValue = string.Empty;
foreach (var prop in TypeDescriptor.GetProperties(typeof(XsdClass)).Cast<PropertyDescriptor>().Where(x => !x.IsReadOnly && x.PropertyType == typeof(string)))
overrides.Add(typeof(XsdClass), prop.Name, attribs);
XmlSerializer serializer = new XmlSerializer(typeof(XsdClass), overrides);
string result;
using (StringWriter textWriter = new StringWriter())
{
serializer.Serialize(textWriter, xc);
result = textWriter.ToString();
}
}
public class XsdClass
{
public string SomeString1 { get; set; }
public string SomeString2 { get; set; }
public string SomeString3 { get; set; }
}
I need to create a lot of XML files. They are very similar and they need to have the following schema:
<Servidor>
<VersaoLayout>0001</VersaoLayout> <!--Will not change-->
<CodigoUJ>001001</CodigoUJ> <!--Will not change-->
<ItemServidor> <!--A list of <T> that will change-->
<CPFServidor>13579024681</CPFServidor>
<NomeServidor>Fulano Pereira Tal</NomeServidor>
</ItemServidor>
</Servidor>
Note that the tags <VersaoLayout> and <CodigoUJ> are the same for all the files i need to create, the <ItemServidor> is the content that will vary. So i thought: Why not create just one class and use generics to handle the differences for this case? And for now my classes are the following:
//Kind of a container class that will hold the content that will vary.
[XmlRoot("Servidor", ElementName="Servidor")]
public class LayoutArquivoXML<T> where T : ItemLayout
{
[XmlElement("VersaoLayout")]
public string VersaoLayout { get; set; }
[XmlElement("CodigoUJ")]
public string CodigoUJ { get; set; }
//[XmlArrayItem("ItemServidor")]
public List<T> ItensLayout { get; set; }
// Constructors omited for simplicity
}
//A "ContentClass". I will have a bunch of classes similar to this
[XmlRoot("ItemServidor", ElementName = "ItemServidor")]
public class ServidorLayout : ItemLayout
{
[XmlElement("CPFServidor")]
public string CPFServidor { get; set; }
[XmlElement("NomeServidor")]
public string Nome { get; set; }
}
I am instantiating my "container class" this way:
LayoutArquivoXML<ServidorLayout> layout = new Layout.LayoutArquivoXML<ServidorLayout>("versao01", "999", lstItensLayout.ToList());
And my Serialization method is the following:
public string Serializador<T>(T objeto)
{
string xml = string.Empty;
using (var sw = new ISO8859StringWriter())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // to omit XML namespaces
var xs = new XmlSerializer(typeof(T));
xs.Serialize(sw, objeto, ns);
}
return xml;
}
These objects graph are generating a XML with the following schema:
<Servidor>
<VersaoLayout>versao01</VersaoLayout>
<CodigoUJ>999</CodigoUJ>
<ItensLayout>
<ServidorLayout>
<CPFServidor>4252813450</CPFServidor>
<NomeServidor>Antonio de Sousa Muniz</NomeServidor>
</ServidorLayout>
</ItensLayout>
</Serv>
I dont want the tag <ItensLayout> tag in the XML. What i need to do to generate the XML in the desired schema?
Also, the root tag <Servidor> will need to change according to the file i am generating, imagine that i need to create another file where the root tag is Another, let say:
LayoutArquivoXML<AnotherLayout> layout = new Layout.LayoutArquivoXML<AnotherLayout>("versao01", "999", anotherItensLayout.ToList());
There are two separate issues.
I don't want the tag <ItensLayout> tag in the XML. What i need to do to generate the XML in the desired schema?
As explained in the documentation for Serializing an Array as a Sequence of Elements, to serialize an array (list, enumerable etc.) as flat sequence of XML elements, you need to apply XmlElement attribute to the property/field.
Also, the root tag <Servidor> will need to change according to the file i am generating
This is controlled by the XmlRoot attribute.
Now, all this applies if you are using concrete classes. In order to apply them dynamically to your generic class, you can use the XmlOverrides class and the XmlSerializer constructors accepting such parameter.
To do that, first add an optional parameter to your original method:
public string Serializador<T>(T objeto, XmlAttributeOverrides overrides = null)
{
string xml = string.Empty;
using (var sw = new ISO8859StringWriter())
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", ""); // to omit XML namespaces
var xs = new XmlSerializer(typeof(T), overrides);
xs.Serialize(sw, objeto, ns);
xml = sw.ToString();
}
return xml;
}
Then create a specific method like this:
public string Serializador<T>(LayoutArquivoXML<T> objeto, string rootElementName, string contentElementName)
where T : ItemLayout
{
var xmlAttrOverrides = new XmlAttributeOverrides();
// Root element name override
var xmlRootAttrs = new XmlAttributes();
xmlRootAttrs.XmlRoot = new XmlRootAttribute(rootElementName);
xmlAttrOverrides.Add(typeof(LayoutArquivoXML<T>), xmlRootAttrs);
// Content element name override
var xmlContentAttrs = new XmlAttributes();
xmlContentAttrs.XmlElements.Add(new XmlElementAttribute(contentElementName));
xmlAttrOverrides.Add(typeof(LayoutArquivoXML<T>), "ItensLayout", xmlContentAttrs);
// Call the original method passing the overrides
return Serializador(objeto, xmlAttrOverrides);
}
Now you can achieve your goal by using something like this:
var source = new LayoutArquivoXML<ServidorLayout>
{
VersaoLayout = "0001",
CodigoUJ = "001001",
ItensLayout = new List<ServidorLayout>
{
new ServidorLayout
{
CPFServidor = "13579024681",
Nome = "Fulano Pereira Tal",
},
}
};
var xml = Serializador(source, "Servidor", "ItemServidor");
which produces:
<Servidor>
<VersaoLayout>0001</VersaoLayout>
<CodigoUJ>001001</CodigoUJ>
<ItemServidor>
<CPFServidor>13579024681</CPFServidor>
<NomeServidor>Fulano Pereira Tal</NomeServidor>
</ItemServidor>
</Servidor>
TL, DR: we've been serializing some data in some tables of our SQL DB. Unfortunately that serialization technique wastes a lot of space for markup characters. We've found a new, more efficient way: to keep backwards compatibility, is it safe to adopt the following logic? -> When the serialization occurs, it always occurs using the new, more efficient way. When deserialization occurs, we check whether the string uses the new serialization format or the old one --> we then deserialize with the appropriate method. Is this robust? Can this be used in production? Aren't there any subtle problems with this approach?
Greetings. I'm working on an application which interacts with a SQL database. To achieve a specific business requirement, we've been serializing some data in a special column of our DB tables, of type ntext. Basically, in each cell of this column, we serialize an array of "Attributo" object, so typeof(T) is Attributo[]:
The "Attributo" definition is like the following:
public class Attributo
{
public virtual String Nome { get; set; }
public virtual String Valore { get; set; }
public virtual String Tipologia { get; set; }
}
- Deserialization to read the actual values:
XMLUtilities.Deserialize<Attributo[]>(value));
Serialization to store the values in the column (for each row..):
XMLUtilities.Serialize(attributes.ToArray());
And this is the helper class, which makes use of the XmlSerializer object:
public static class XMLUtilities
{
public static T Deserialize<T>(String xmlString)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlString))
{
return (T) serializer.Deserialize(reader);
}
}
public static String Serialize<T>(T xmlObject)
{
MemoryStream stream = new MemoryStream();
XmlSerializer serializer = new XmlSerializer(typeof(T));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");
serializer.Serialize(stream, xmlObject, xmlnsEmpty);
stream.Seek(0, SeekOrigin.Begin);
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
Now, the problem with this technique is that it wastes a lot of space for markup characters. This is an example string which is stored on the db:
<?xml version="1.0"?>
<ArrayOfAttributo>
<Attributo>
<Nome>Leakage_test_Time_prg1_p1</Nome>
<Valore>4</Valore>
<Tipologia>Single</Tipologia>
</Attributo>
<Attributo>
<Nome>Leakage_air_Volume_p1</Nome>
<Valore>14</Valore>
<Tipologia>Single</Tipologia>
</Attributo>
</ArrayOfAttributo>
So, we've found a more concise way of serializing these Attributo[], which produces this kind of output:
<ArrayOfA xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<A>
<N>Leakage_test_Time_prg1_p1</N>
<V>4</V>
<T>Single</T>
</A>
<A>
<N>Leakage_air_Volume_p1</N>
<V>14</V>
<T>Single</T>
</A>
</ArrayOfA>
then, to preserve backwards compatibility, which is the core issue we have implemented the following logic:
During serialization:
we always serialize in the new, more concise fashion
During deserialization:
we check whether the string starts with:
<?xml version="1.0"?>
or not. If that is the case, this is an old entry so we deserialize it in the old way. Otherwise, we deserialize using the new format.
We achieved that by decorating "Attributo" this way:
[DataContract(Name = "A", Namespace= "")]
public class Attributo
{
[DataMember(Name = "N")]
public virtual String Nome { get; set; }
[DataMember(Name = "V")]
public virtual String Valore { get; set; }
[DataMember(Name = "T")]
public virtual String Tipologia { get; set; }
}
and by performing the following changes to our serialization/deserialization methods, which now, for the new serialization technique, rely on DataContractSerializer object:
public static T Deserialize<T>(String xmlString)
{
//let's see if it's an old-style entry...
if (xmlString.StartsWith("<?xml version=\"1.0\"?>\r\n<ArrayOfAttributo>"))
{
try
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (TextReader reader = new StringReader(xmlString))
{
return (T)serializer.Deserialize(reader);
}
}
catch { }
}
//..then it must be a new-style one
DataContractSerializer ser = new DataContractSerializer(typeof(T));
using (Stream s = _streamFromString(xmlString))
{
return (T) ser.ReadObject(s);
}
}
public static String Serialize<T>(T xmlObject)
{
MemoryStream stream1 = new MemoryStream();
DataContractSerializer ser = new DataContractSerializer(typeof(T));
ser.WriteObject(stream1, xmlObject);
stream1.Position = 0;
StreamReader sr = new StreamReader(stream1);
string xmlString = sr.ReadToEnd();
return xmlString;
}
private static Stream _streamFromString(string s)
{
MemoryStream stream = new MemoryStream();
StreamWriter writer = new StreamWriter(stream);
writer.Write(s);
writer.Flush();
stream.Position = 0;
return stream;
}
Everything seems to be working with this approach but we want to assess every possible risk before proceeding any further. Is this safe to use in production?
One more thing to keep in mind, while deserializing the older entry :
deserialize old-entry in old-style
serialize the old-entry in new-style
save the new-style-serialized-old-entry, and delete the old-style-serialized-old-entry.
You're good to go.
We are using XMLSerializer.Deserialize(XMLReader) on .Net 4.5 to deserialize XML into an Object Graph generated by xsd.exe.
I would like to know what the expected deserialization behavior is for an array which is annotated with the XMLElementAttribute - specifically with regard to ordering. For example:
For the following property:
[System.Xml.Serialization.XmlElementAttribute("GivenName")]
public string[] GivenName {
// get() and set() methods
}
And the following XML:
<root>
<GivenName>One</GivenName>
<GivenName>Two</GivenName>
<GivenName>Three</GivenName>
</root>
Will this always deserialize as ['One', 'Two', 'Three']
So that the array order always matches the XML order
Also is there any documentation I can reference that clearly states this.
Thanks
Rob
Yes Its deserialize same order. Check the code sample code and output:
[System.SerializableAttribute()]
public class SampleClass
{
[System.Xml.Serialization.XmlElementAttribute(Order = 10)]
public string Foo { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Order = 5)]
public string Bar { get; set; }
[System.Xml.Serialization.XmlElementAttribute("GivenName", Order = 15)]
public string[] GivenNames { get; set; }
}
class Program
{
static void Main(string[] args)
{
string[] names = new string[3] { "One", "Two", "Three" };
SampleClass TestObj = new SampleClass { Bar = "dsdfsdf", Foo = "test", GivenNames = names };
XmlSerializer SerializerObj = new XmlSerializer(typeof(SampleClass));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(#"C:\files\test.xml");
SerializerObj.Serialize(WriteFileStream, TestObj);
// Cleanup
WriteFileStream.Close();
// Create a new file stream for reading the XML file
FileStream ReadFileStream = new FileStream(#"C:\files\test.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
// Load the object saved above by using the Deserialize function
SampleClass LoadedObj = (SampleClass)SerializerObj.Deserialize(ReadFileStream);
// Cleanup
ReadFileStream.Close();
}
}
You can find some awesome information about it on msdn which is provide by microsoft.
This is the "official" documentation
The serialization is linear so you will alway get the same order normally.
I have the following class and data members (plus irrelevant methods) I am new to XML and .NET (that excuse is getting old though) and have spent a couple of days reading the MSDN entries (and whatever google turned up) for first XMLReader, then XMLDocument and now XDocument, XElement and XNode but am no closer to a concrete solution to serializing my class (perhaps I need to study serialization in .NET in more depth). To get me started I have some data in an XML file that I want to read (although it prolly is in the wrong format) in to initialize a class to initialize my application. The configuration class is as follows:
class IWantToFile
{
class DirsAndFiles
{
public List<string> Suffixes;
public string Dir;
}
enum OPOptsEnum
{
op1Description, op2Description, op3Description, op4Description,
op5Description, op6Description, op7Description, op8Description,
};
List<DirsAndFiles> ProjectDirs;
bool[] OPOpts = new bool[(int)OPOptsEnum.op8Description + 1];
bool otherOpt;
}
Observing the one to one and one to many relationships therein (eg List<DirsAndFiles> ProjectDirs) can someone please give concise methods to read and write this data to a file? It would greatly assist my development in these fields.
I've got as far as:
if (File.Exists(SPECFILENAME)) {
XDocument xdoc = XDocument.Load(SPECFILENAME);
//Ummm.....
}
but then my lack of .NET XML and Linq exeperience fail me.
I think you might want to use the XmlSerializer, which 'Serializes and deserializes objects into and from XML documents'?
See How to serialize an object to XML by using Visual C# for sample code.
[Serializable]
public class MyObject
{
public string SerializeMe { get; set; }
[XmlIgnore]
public string DONTSerializeMe { get; set; }
}
Helper....
public static class SerializerHelper<T>
{
public static string Serialize(T myobject)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringWriter stringWriter = new StringWriter();
xmlSerializer.Serialize(stringWriter, myobject);
string xml = stringWriter.ToString();
return xml;
}
public static T Deserialize(string xml)
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
StringReader stringReader = new StringReader(xml);
return (T)xmlSerializer.Deserialize(stringReader);
}
}
Usage
MyObject myObject = new MyObject();
string xml = SerializerHelper<MyObject>.Serialize(myObject);
MyObject DeserializedObject = SerializerHelper<MyObject>.Deserialize(xml);