Currently, many xml tool software can write xml documents according to the xsd file book ,. net does not implement this method. if I have written several methods for browsing, verifying, and creating xml, many xml tool software can write xml documents according to the xsd file book ,. net does not implement this method. for example, I have written several methods for browsing, verifying, and creating xml.
The code is as follows:
Using System; using System. data; using System. configuration; using System. web; using System. web. security; using System. web. UI; using System. web. UI. webControls; using System. web. UI. webControls. webParts; using System. web. UI. htmlControls; using System. xml; using System. xml. schema; using System. collections ;////// Summary of ProXML ///Public class ProXml {public ProXml () {// TODO: add the constructor logic here //}////// Obtain the xsd file path ///Public static string GetSchemaPath {get {return HttpContext. Current. Request. PhysicalApplicationPath + "App_Data \ system \ publish. xsd ";}}////// Authorized node //////
Public static System. Collections. Generic. List
GetDatas () {XmlSchemaSet xsSet = new XmlSchemaSet (); xsSet. add ("http://www.w3.org/2001/XMLSchema", GetSchemaPath); xsSet. compile (); XmlSchema schema = null; foreach (XmlSchema xs in xsSet. schemas () {schema = xs;} System. collections. generic. list
Elements = new System. Collections. Generic. List
(); Foreach (XmlSchemaObject obj in schema. elements. values) {if (obj. getType () = typeof (XmlSchemaElement) {elements. add (XmlSchemaElement) obj) ;}} return elements ;}///
/// Add element //////
///
///
///
///
Public static 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 = (Excel. range) st. cells [rowIndex, (int) titles [name]). value2.ToString (); 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 ;}}///
/// Obtain the element //////
///
Public static System. Collections. Generic. List
GetDataItem (string name) {System. Collections. Generic. List
Arr = new System. Collections. Generic. List
(); 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 static void GetItem (System. collections. generic. list
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 ;}}///
/// Obtain the node based on the node name //////
///
Public static XmlSchemaElement GetTableSchema (string name) {XmlSchemaSet xsSet = new XmlSchemaSet (); xsSet. add ("http://www.w3.org/2001/XMLSchema", GetSchemaPath); xsSet. compile (); XmlSchema schema = null; foreach (XmlSchema xs in xsSet. schemas () {schema = xs;} XmlQualifiedName qf = new XmlQualifiedName (name, "http://www.w3.org/2001/XMLSchema"); 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 ;}}///
/// Check the dom object //////
///
Public static string CheckDataXml (XmlDocument doc) {XmlSchemaSet xsd = new XmlSchemaSet (); xsd. add ("", GetSchemaPath); doc. schemas = xsd; try {doc. validate (new ValidationEventHandler (XmlValidation);} catch (Exception ex) {return ex. message;} return null ;}}
The preceding section describes the sample code analysis for generating xml documents based on xsd. For more information, see other related articles in the first PHP community!