XML Object Serialization on Collection At Root Level - c#

Trying to deserialize an XML file from a 3rd party tool into a custom software class. The problem is that the XML is a little goofy as the "root" item is really the declaration of the collection. I have done a lot with XMLArray and other definitions to define serialization, but this does not seem to work. The best I have been able to do is to get the collection read in but none of the objects in the collection are initialized with the variables.
Here is an example of what I am facing:
XML:
<Animals>
<Animal>
<Name>Mr. Cow</Name>
<Type>Cow</Type>
</Animal>
<Animal>
<Name>Belle</Name>
<Type>Cow</Type>
</Animal>
<Animal>
<Name>Porky</Name>
<Type>Pig</Type>
</Animal>
</Animals>
I then have my XMLRoot defined as "Animals" in my "Farm" object. And have a list property for my class of "Animal". If I define that property (list AnimalCollection) the best I can get is a list of 3 uninitiated Animal objects (none of the values set).
Around the office, the best we could figure to do was to add a node to the file as a stream before trying to deserialize it and use it business as usual, but obviously this seems like a horrible hack. Hoping that there is a better way that I am just missing.
I have also thought about manually reading the document as well... but again hoping there is a more elegant way.

XmlSerializer serializer = new XmlSerializer(typeof(Animal[]), new XmlRootAttribute("Animals"));
public class Animal
{
[XmlElement]
public string Name;
[XmlElement]
public string Type;
}

Related

XML - Serialize class - Some questions

I have the idea of serializing the properties of a class to a XML-file and then upload this file to a server. This is because I want to download this file again and read out the serialized content.
So a structure like this:
File exists (myfile.xml) -> Serialize data from a class (myclass.cs) to the file -> Upload the file.
Now the content could look something like this:
<?xml version="1.0"?>
-<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeProperty>0.1.0.0</SomeProperty>
<SomePropertiesChild>Test</SomePropertiesChild>
</MyClass>
Well, so this is not the problem, I have built a class to serialize the data to the file, so that it looks like above.
The problem now is, that I do not know how to go on. I want that the next time new data will be serialized like that, but it should be attached to that XML-file, not a new one.
So I download this file and then serialize new data as attachment to it, so that it will look the following:
<?xml version="1.0"?>
-<MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SomeProperty>0.1.0.0</SomeProperty>
<SomePropertiesChild>Test</SomePropertiesChild>
<!-- This is the part that should be attached -->
<SomeProperty>0.2.0.0</SomeProperty>
<SomePropertiesChild>Test2</SomePropertiesChild>
</MyClass>
And this should be repeated as many times as I want: download file, serialize new data and attach it to this one, then upload it again.
Now, the second point is, that I want to read this out.
For that I use the XML-class, too. For the type param for the type that the data should be serialized to, I set the class that contains the properties.
But now, I have more nodes of the same name in my XML like above. I want to separate this now.
So, not only that one class, but for each node with the same name a new class.
To show you that a bit clearer:
In the XML we have the first pair (SomeProperty, SomePropertiesChild).
This one should be deserialized to myclass.cs now, which contains the right properties for the XML-nodes. Example:
public class MyClass
{
public Version SomeProperty {get; set;}
public string SomeProeprtiesChild {get; set;}
}
And then the first XML-pair should be deserialized to that class.
But I want to do this for each XML-"pair" in my XML-file, but I can't get a right structure in my head.
Should I make a List here or how should I do that?
I really hope I could explain it a bit clearly, it is very difficult to explain.
Also I do not know how to do this with the attaching. Any tips?
If you have questions, ask!
What you are looking for is not simply serialization. You want to serialize the same object more than once right?
A possible solution is to serialize a List<MyClass> and append new instances to the list if the file exists.
In short, there is no built-in way to do what it is you are looking for. However, that doesn't mean it can't be done...
The simplest, and closest built-in, way would be to maintain a List<MyClass> which you update whenever you upload/download etc. and serialize. However, this wouldn't get the exact XML you want, this would produce something along the lines of
<ListOfMyClass>
<MyClass>
<Property>1</Property>
...
</MyClass>
<MyClass>
<Property>2</Property>
...
</MyClass>
</ListOfMyClass>
The more complex approach would be to have MyClass implement IXmlSerializable and override the behaviour of WriteXml/ReadXml to handle your custom XML format.

Asp.net XML to objects

I have a XML file, with a structure like
<items>
<item>
<someDetail>
A value here
</someDetail>
</item>
<item>
<someDetail>
Another value here
</someDetail>
</item>
</items>
With multiple items in it.
I want to deserialize the XML on session start ideally, to turn the XML data to objects based on a class (c# asp.net 4)
I have tried several ways with either no success, or a solution which seems clunky and inelegant.
What would people suggest?
I have tried using the xsd.exe tool, and have tried with the xml reader class, as well as usin XElement class to loop through the xml and then create new someObject(props).
These maybe the best and/or only way, but with it being so easy for database sources using the entities framework, I wondered if there was a similar way to do the same but from a xml source.
The best way to deserialize XML it to create a class that corresponds to the XML structure into which the XML data will deserialize.
The latest serialization technology uses Data Contracts and the DataContractSerializer.
You decorate the class I mentioned above with DataMember and DataItem attributes and user the serializer to deserialize.
I'd use directly the .NET XML serialization - classes declarations:
public class Item {
[XmlElement("someDetail")]
public string SomeDetail;
} // class Item
[XmlRoot("items")]
public class MyData {
[XmlElement("item")]
public List<Item> Items;
public static MyData Deserialize(Stream source)
{
XmlSerializer serializer = new XmlSerializer(typeof(MyData));
return serializer.Deserialize(source) as MyData;
} // Deserialize
} // class MyData
and then to read the XML:
using (FileStream fs = new FileStream(#"c:\temp\items.xml", FileMode.Open, FileAccess.Read)) {
MyData myData = MyData.Deserialize(fs);
}
I've concluded is there is not simple unified mechanism (probably due to the inherent complexities involved with non trivial cases - this question always crops up in the context of simple scenarios like your example xml).
Xml serialization is pretty easy to use. For your example, you would just have to create a class to contain a items and another class for the actual item. You might have to apply some attributes to get everything to work correctly, but the coding will not be much. Then it's as easy as -
var serializer = new XmlSerializer(typeof(ItemsContainer));
var items = serializer.Deserialize(...) as ItemsContainer;
Datasets are sometimes considered "yesterday tech" but I use them when they solve the problem well, and you can leverage the designer. The generated code is not pretty but the bottom line is you can persist to a database via the auto generated adapters and to XML using a method right on the data set. You can read it in this way as well.
XSD.exe isn't that bad once you get used to it. I printed the help to a text file and included it in my solutions for a while. When you use the /c option to create classes, you get clean code that can be used with the XmlSerialzier.
Visual Studio 2010 (maybe other versions too) has an XML menu which appears when you have an Xml file open and from that you can also generate an XSD from sample Xml. So in a couple of steps you could take your example xml and generate the XSD, then run it through XSD.exe and use the generated classes with a couple of lines XmlSerializer code... it feels like a lot of machinations but you get used to it.

Can I add [XmlElement] attribute to List members without breaking backwards compatability?

I believe the following:
public List<Vector3> Vectors;
Will serialize out to:
<Vectors>
<Vector3>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</Vector3>
</Vectors>
I want to remove the encasing tag which I believe I can do like this:
[XmlElement("Vector3")]
public List<Vector3> Vectors;
Which should serialize to:
<Vector3>
<X>0</X>
<Y>0</Y>
<Z>0</Z>
</Vector3>
But I'm afraid that would break old XML files that are still using the "Vectors" tag around the list. Is there a common way to solve this?
EDIT: The list above would be part of a container object, so the full XML might begin with
<Container>
and end with
</Container>
I left that out originally to keep the question shorter.
I don't believe XML has any sort of built in mechanism for versioning. I think your best bet is going to be writing some external mechanism which can detect the "version" as defined by you and deserialize the old version into your new object manually. You probably will also want to define a new version member variable or property which will serialize with your object in case you run into the same problem again, because once you change the schema a 2nd time, you will have 3 versions to worry about.
You can either write a custom deserialize method by defining IXmlSerializable on your object and defining the readXml/writeXml functions, or you can use some external process to generate the new XML format based on the old version. Perhaps load the XML file into an XmlDocument first, fix it how you want (i.e. move the Vector3 nodes up a level and remove the Vectors node), then save the document's OuterXml value into a string and deserialize via a MemoryStream.

What is the best way to read an xml file when there is no standard schema?

I am working on an application in which, i have to read XML files that have a different set of nodes each time, although only a certain number of nodes appear in all the files, the combination in which they appear keep on changing, the XML files are generated by another system which i cannot control, I am looking into Linq to XML and XML serialization, but i guess serialization is not a choice since it needs pre-built classes to create objects.
Example XML data
<Employee>
<PersonalInfo>
<FirstName>Vamsi</FirstName>
<LastName>Krishna</LastName>
</PersonalInfo>
<EmploymentInfo>
<Department>
<Id>101</Id>
<Position>SD</Position>
<Department>
<EmploymentInfo>
</Employee>
Another Format
<Employee>
<PersonalInfo>
<FirstName>Vamsi</FirstName>
<LastName>Krishna</LastName>
</PersonalInfo>
</Employee>
You can observe that EmploymentInfo node is completely missing in the second example, there are many number of combinations in which the XML data can be presented to the application, I have to read the XML file validate it insert into an SQL Server database through my C# code.
I'd say it depends.
If you just want to communicate with another system in a strongly-typed way, and you can expect the XML schemas to not be changing very frequently, you might be OK with XML serialization. Just encapsulate the deserialization into a separate component and write different versions of them (yes, you'll need to be able to determine the schema version that is currently used). I mean, each version would have it's own set of classes that are targeted by the serializer.
But if you really cannot infer a system out of the schemas used by the external app and need some intelligent parser, you'd better use XPath or Linq to XML or some other XML-level APIs to manually handle the XML-s.
BTW, both of your samples are pretty easy for the XMLSerializer. In the second case it will just set EmploymentInfo to null.
You could write a parser class wich ueses .Net Xpath implementation. The parser should test the child elements for specific nodes before processing the data.
Visit MSDN for the complete syntax.
Update
A little example what i would do to solve the problem. At first, some Model classes to hold some data:
public class PersonalInfo
{
public string FirstName { get; set;}
public string LastName { get; set;}
// more properties
}
public class EmployeeModel
{
// remove List<> if you always just have 1 personalinfo child element
public List<PersonalInfo> {get; set;}
public List<EmploymentInfo> {get; set;}
// more properties
}
Now your "Parser":
public class MyParser
{
// load xml string or xml file in constructor
public MyParser(string xmlSource) { .. }
public EmployeeModel GetEmployeeModel()
{
var result = new EmployeeModel();
// use what ever you want to select nodes from your xml
// and set data of result
return result;
}
}
In your productive code you can use this parser class to get a model of your xml data.

Class design to contain data from an XML file

I'm a student working on a lab that parses a pseudo XML file(basically coded our own parser) for data, stores the retrieved elements and data values, and displays (next lab will be adding "add,change,delete" functionality)
I was thinking about holding this read in information in some sort of multidimensional List due to it being dynamic by default. The other suggestion I've read over some other questions here at SO is to make a "parent node" class, and just store that in an array.
The problem I have is that at code time there is no way to know for sure how many child nodes a parent node will have. It could be --
<parent>
<child1>data value</child1>
<child2>data value</child2>
...etc
</parent>
or
<parent>
<child1data value</child1>
</parent>
I can't really think how I could code a class to have an unknown amount of variables.
Why not just use a List<List<T>>? Or maybe a Dictionary<string, List<T>>, assuming your parent nodes have unique identifiers?
You can keep a list of nodes or a dictionary of nodes if the nodes are unique in some way.
.NET collections are inherently dynamic. You don't need to know in advance how many items your collection will hold. A collection of your own class "parent" would work. Each parent class itself would implement a collection of "children". You can define them to be anything you want.
Even better though would be to make your class serializable from the get go, so that when you save it to an XML file it's already in a properly formatted XML structure. Reading in that data would require deserialization and it would populate everything for you. Check this out.
You should manage by your self but just think, you have parent as root and parent has list of child. Where is problem ? :)

Categories

Resources