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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.