XML parsing by XSD

Source: Internet
Author: User
Tags xml example

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.




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>



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.