The following describes how to generate a serialized deserialization class based on XML. xml must be first converted to XSD and then regenerated into an object class. XSD is the abbreviation of XML schema definition.
1. Have an XML file
2. Open command line of:
XSD *. xml: execute this command to generate the XSD file through the XML file. The XSD file will be generated under the current command directory.
XSD *. XSD/classes is an XML parsing class generated through the XSD file, CS file. By the way, the namespace is specified as test.
In this way, the CS file can be put into the project for use. This CS file is the parsing class corresponding to XML.
PS: automatically parsed XSD files do not know if each node is unique or has multiple files. If you need to specify that a node is unique, you can put the XSD file into vs After generating the XSD file,
Open in XML (text) Editor mode and specify maxoccurs = "1", so that the current node is unique. Maxoccurs = "unbounded" indicates that there can be multiple XML nodes. In this way, the modified XSD will use the XSD *. XSD/classes command to generate the CS file, which may better meet your needs.
3. parse XML
Static void main (string [] ARGs) {try {xmlserializer SER = new xmlserializer (typeof (response); // read XML for parsing filestream FS = new filestream (@ "D: \. XML ", filemode. open); Response response = Ser. deserialize (FS) as response; response. sip. line1.port = "9999"; FS. close (); // modify XML and save FS = new filestream (@ "D: \. XML ", filemode. open, fileaccess. write); Ser. serialize (FS, response); FS. close ();} catch (exception ex) {console. writeline (ex. message );}}
Response is the first node of my XML.
Additional:
My XML example is:
<Response> <APIVersion>1.0</APIVersion> <Authenticated>true</Authenticated> <Global> <SoftwareUpgrade> <Enabled>false</Enabled> <Version/> <URL/> <Rule/> <RetryInterval>3600</RetryInterval> </SoftwareUpgrade> <Provisioning> <Enabled>true</Enabled> <URL>https://eeee</URL> <Interval>86400</Interval> </Provisioning> <Phonebook> <Enabled>true</Enabled> <URL>https://ffff</URL> </Phonebook> </Global> <SIP> <Line1> <DisplayName>Terry Tang</DisplayName> <URI>553870769</URI> <PreferredURI>[email protected]</PreferredURI> <AuthName>553870769</AuthName> <Password>cJFZZjUsPU</Password> <Transport>TCP</Transport> <Mailbox> 123 </Mailbox> <Port>5060</Port> <Proxy> <AutoMode>off</AutoMode> <Proxy1>proxy.infocus.net</Proxy1> <Proxy2/> </Proxy> </Line1> </SIP></Response>