using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml; namespace RWXML { /**/ /// <summary> /// TransXSD 的摘要说明。 /// </summary> public class TransXSD { public static string strTitle = "<?xml version=/"1.0/" encoding=/"utf-8/" ?>/n/r"; public static string strFirst = "<node>/n/r"; public static string strLast = "</node>"; public static bool result = false; public TransXSD() { // // TODO: 在此处添加构造函数逻辑 // } /**/ /// <summary> /// Check the childnodes if exist element "xs:element" /// </summary> /// <param name="node"></param> /// <returns></returns> public static bool CheckNode(XmlNode node) { foreach (XmlNode nodeC in node.ChildNodes) { if (nodeC.Name == "xs:element") { result = true; } else { if (nodeC.ChildNodes.Count > 0) { CheckNode(nodeC); } } } return result; } public static void InitialResult() { result = false; } /**/ /// <summary> /// Get the xml text /// </summary> /// <param name="node"></param> public static void GetNodes(XmlNode node) { if (node.HasChildNodes) { if (node.Name == "xs:element") { if (CheckNode(node)) { InitialResult(); strFirst += "<" + node.Attributes["name"].InnerXml + ">/n/r"; } else { string temp1 = ""; temp1 += "<" + node.Attributes["name"].InnerXml + ">" + "</" + node.Attributes["name"].InnerXml + ">/n/r"; strFirst += temp1; } } if (CheckNode(node) == true) { foreach (XmlNode nodeC in node.ChildNodes) { InitialResult(); GetNodes(nodeC); } if (node.Name == "xs:element") { strFirst += "</" + node.Attributes["name"].InnerXml + ">/n/r"; } } } else { if (node.Name == "xs:element") { string temp1 = ""; temp1 += "<" + node.Attributes["name"].InnerXml + ">" + "</" + node.Attributes["name"].InnerXml + ">/n/r"; strFirst += temp1; } } } /**/ /// <summary> /// Build the text for xml from *.xsd /// </summary> /// <param name="strXSD">The *.xsd file address</param> /// <param name="strXML">The xml stord address</param> public void BuildTextandXML(string strXSD, string strXML) { BuildText(strXSD); BuildXML(strXML); } public static void BuildText(string strXSD) { XmlDocument doc = new XmlDocument(); doc.Load(strXSD); XmlNodeList list = doc.DocumentElement.ChildNodes; foreach (XmlNode node in list) { GetNodes(node); } } public static void BuildXML(string strXML) { StreamWriter sw = new StreamWriter(strXML); try { sw.WriteLine(strTitle + strFirst + strLast); } catch (Exception ex) { throw new Exception(ex.Message); } finally { sw.Close(); } } } }