Add a class that generates XML based on XSD to favorites

Source: Internet
Author: User

A class provided by a friend, providing methods to parse XSD

You can add nodes to xmldoc Based on XSD to generate a complete XML object.

As follows:

Using system; using system. data; using system. configuration; using system. web; using system. XML; using system. XML. schema; using system. collections; /** /// <summary> // Summary of proxml // generate an XML document based on XSD /// </Summary> namespace rectified {public class proxml {private static string apppath; public proxml (string runpath) {apppath = runpath; // todo: add the constructor logic //}/** // <summary> // obtain the XSD file path /// </Summary> Public static string getschemapath {get {return apppath + "rectifieddcdata. XSD ";}} /** //// <summary> /// obtain the node // </Summary> /// <returns> </returns> public system. collections. generic. list <xmlschemaelement> getdatas () {xmlschemaset xsset = new xmlschemaset (); xsset. add ("http:/www.mpfa.org.hk/rectifieddcdata/", getschemapath); xsset. compile (); XMLSCHEMA schema = NULL; foreach (XMLSCHEMA Xs in xsset. schemas () {schema = Xs;} system. collections. generic. list <xmlschemaelement> elements = new system. collections. generic. list <xmlschemaelement> (); foreach (xmlschemaobject OBJ in schema. elements. values) {If (obj. getType () = typeof (xmlschemaelement) {elements. add (xmlschemaelement) OBJ) ;}} return elements ;} /** // <summary> // Add an element // </Summary> /// <Param name = "sourcexsd"> </param> /// <Param name = "titles"> </param> // <Param name = "sourcexml"> </param> // <Param name = "sourcend"> </ param> // <Param name = "values"> </param> Public void addelement (xmlschemaobject sourcexsd, hashtable titles, xmldocument sourcexml, xmlnode sourcend, string [] values) {If (sourcexsd. getType () = typeof (xmlschemachoice) {xmlschemachoice choice = sourcexsd as xmlschemachoice; decimal min = choice. minoccurs; foreach (xmlschemaobject item in choice. items) {If (item. getType () = typeof (xmlschemaelement) {string name = (xmlschemaelement) item ). name; If (titles. containskey (name) {xmlelement element = sourcexml. createelement (name); element. innertext = values [(INT) Titles [name]; sourcend. appendchild (element) ;}} else {addelement (item, titles, sourcexml, sourcend, values) ;}} else if (sourcexsd. getType () = typeof (xmlschemaelement) {string name = (xmlschemaelement) sourcexsd ). name; If (titles. containskey (name) {xmlelement element = sourcexml. createelement (name); element. innertext = values [(INT) Titles [name]; sourcend. appendchild (element) ;}} else if (sourcexsd. getType () = typeof (xmlschemasequence) {foreach (xmlschemaobject childitem in (xmlschemasequence) sourcexsd ). items) {If (childitem. getType () = typeof (xmlschemaelement) {string name = (xmlschemaelement) childitem ). name; If (titles. containskey (name) {xmlelement element = sourcexml. createelement (name); element. innertext = values [(INT) Titles [name]; sourcend. appendchild (element) ;}} else {addelement (childitem, titles, sourcexml, sourcend, values) ;}} else {return ;}} /** // <summary> // obtain the element // </Summary> /// <Param name = "name"> </param> /// <returns> </returns> public system. collections. generic. list <xmlschemaelement> getdataitem (string name) {system. collections. generic. list <xmlschemaelement> arr = new system. collections. generic. list <xmlschemaelement> (); xmlschemaelement element = gettableschema (name); If (element = NULL) {return NULL;} xmlschemacomplextype complex = element. schematype as xmlschemacomplextype; xmlschemasequence sequence = complex. contenttypeparticle as xmlschemasequence; foreach (xmlschemaobject OBJ in sequence. items) {If (obj. getType () = typeof (xmlschemaelement) {xmlschemaelement item = (xmlschemaelement) OBJ; arr. add (item) ;}else {getitem (ARR, OBJ) ;}} return arr;} public void getitem (system. collections. generic. list <xmlschemaelement> arr, xmlschemaobject OBJ) {If (obj. getType () = typeof (xmlschemaelement) {xmlschemaelement item = (xmlschemaelement) OBJ; arr. add (item);} else if (obj. getType () = typeof (xmlschemachoice) {xmlschemachoice choice = OBJ as xmlschemachoice; foreach (xmlschemaobject child in choice. items) {If (child. getType () = typeof (xmlschemaelement) {xmlschemaelement item = Child as xmlschemaelement; arr. add (item) ;}else {getitem (ARR, child) ;}} else if (obj. getType () = typeof (xmlschemasequence) {xmlschemasequence sequence = OBJ as xmlschemasequence; foreach (xmlschemaobject child in sequence. items) {If (child. getType () = typeof (xmlschemaobject) {xmlschemaelement item = Child as xmlschemaelement; arr. add (item) ;}else {getitem (ARR, child) ;}} else {return ;}} /** // <summary> // obtain the node based on the node name // </Summary> // <Param name = "name"> </param> /// <returns> </returns> Public xmlschemaelement gettableschema (string name) {xmlschemaset xsset = new xmlschemaset (); xsset. add ("http:/www.mpfa.org.hk/rectifieddcdata/", getschemapath); xsset. compile (); XMLSCHEMA schema = NULL; foreach (XMLSCHEMA Xs in xsset. schemas () {schema = Xs;} xmlqualifiedname QF = new xmlqualifiedname (name, "http:/www.mpfa.org.hk/rectifieddcdata/"); If (schema. elements. contains (QF) {return (xmlschemaelement) schema. elements [QF];} return NULL;} static void xmlvalidation (Object sendor, validationeventargs e) {Switch (E. severity) {Case xmlseveritytype. error: Throw E. exception; Case xmlseveritytype. warning: Throw E. exception ;}} /** // <summary> // check the DOM object // </Summary> // <Param name = "Doc"> </param> // /<returns> </returns> Public static string checkdataxml (xmldocument DOC) {xmlschemaset XSD = new xmlschemaset (); XSD. add ("http:/www.mpfa.org.hk/rectifieddcdata/", getschemapath); Doc. schemas = XSD; try {Doc. validate (New validationeventhandler (xmlvalidation);} catch (exception ex) {return ex. message;} return NULL ;}}}

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.