1. How to generate schema file from existed XML file
answer: run command-line under vs2010 tools
switch current location to location that XML is putted in
run: xsd XML
then, a schema with same name with XML is generated.
We also can adjust the schema in XML Editor
2. After change data in previous XML file,
How to re-get the data from it.
Deserialize
3. A method name: LoadXMLonSpecialSchema(Object myobject, string XML)
The flow of it is:
XmlSerializer serailizer = new XMLSerializer( System.Type of Object);
this object is an instance of root class that will be used for store the data from XML contain entire root.
But, how to create his class,
we also can as XSD.exe for help:
xsd name.xsd /classes
Stream streader = new FileStream(XML, FileMode.open);
XMLReader reader = XmlReader.Create(streader);
myobject = serializer.Derialize(reader);
reader.close();
return myobject
It is over.
During finish this i meet some problem:
1. Derailize report error when i use "TestReader"/"StreamReader" instead of Stream/FileStream and XMLReader
2. Derialize report error as below:
unable to generate a temporary class(result=1) and error CS0030 and CS0029.
This is a bug that Microsoft will not fix it.
It happen to this scenario that is:
The schema contain a complex element and in this elment only contain a complex element and whose "maxOccurs" value is "unbounded".
How to avoid it:
Have to add a property <xs:attribute name="tmp" type="xs:string"> to the parent element.
After this, we can get a sample like this:
<targets p2:tmp="tmp1" xmln:p2="http://********">
<p2:target>
<p2:source>sourcefile1</p2source>
<p2:destination>destination2</p2:destination>
</p2target>
<p2:target>
<p2:source>sourcefile1</p2source>
<p2:destination>destination2</p2:destination>
</p2target>
</targets>
All this finished, the deserialized can successfully.