将XSD文件转换成XML文件的源码,望高手优化代码
递归得头都有些痛了。写的有些匆忙,经调试,暂无bug.
1using System;
2using System.IO;
3using System.Xml;
4
5namespace RWXML
6{
7 /// <summary>
8 /// TransXSD 的摘要说明。
9 /// </summary>
10 public class TransXSD
11 {
12 public static string strFirst="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\r";
13 public static string strLast="";
14 public static bool result=false;
15 public TransXSD()
16 {
17 //
18 // TODO: 在此处添加构造函数逻辑
19 //
20 }
21 /// <summary>
22 /// Check the childnodes if exist element "xs:element"
23 /// </summary>
24 /// <param name="node"></param>
25 /// <returns></returns>
26 public static bool CheckNode(XmlNode node)
27 {
28 foreach(XmlNode nodeC in node.ChildNodes)
29 {
30 if(nodeC.Name=="xs:element")
31 {
32 result=true;
33 }
34 else
35 {
36 if(nodeC.ChildNodes.Count>0)
37 {
38 CheckNode(nodeC);
39 }
40 }
41 }
42 return result;
43 }
44 public static void InitialResult()
45 {
46 result=false;
47 }
48 /// <summary>
49 /// Get the xml text
50 /// </summary>
51 /// <param name="node"></param>
52 public static void GetNodes(XmlNode node)
53 {
54 if(node.HasChildNodes)
55 {
56 if(node.Name=="xs:element")
57 {
58 if(CheckNode(node))
59 {
60 InitialResult();
61 strFirst+="<"+node.Attributes["name"].InnerXml+">\n\r";
62 }
63 else
64 {
65 string temp1="";
66 temp1+="<"+node.Attributes["name"].InnerXml+">"+"</"+node.Attributes["name"].InnerXml+">\n\r";
67 strFirst+=temp1;
68 }
69
70 }
71 if(CheckNode(node)==true)
72 {
73 foreach(XmlNode nodeC in node.ChildNodes)
74 {
75 InitialResult();
76 GetNodes(nodeC);
77 }
78 if(node.Name=="xs:element")
79 {
80 strFirst+="</"+node.Attributes["name"].InnerXml+">\n\r";
81 }
82 }
83 }
84 else
85 {
86 if(node.Name=="xs:element")
87 {
88 string temp1="";
89 temp1+="<"+node.Attributes["name"].InnerXml+">"+"</"+node.Attributes["name"].InnerXml+">\n\r";
90 strFirst+=temp1;
91 }
92 }
93 }
94 /// <summary>
95 /// Build the text for xml from *.xsd
96 /// </summary>
97 /// <param name="strXSD">The *.xsd file address</param>
98 /// <param name="strXML">The xml stord address</param>
99 public static void BuildTextandXML(string strXSD,string strXML)
100 {
101 BuildText(strXSD);
102 BuildXML(strXML);
103 }
104 public static void BuildText(string strXSD)
105 {
106 XmlDocument doc=new XmlDocument();
107 doc.Load(strXSD);
108 XmlNodeList list=doc.DocumentElement.ChildNodes;
109 foreach(XmlNode node in list)
110 {
111 GetNodes(node);
112 }
113 }
114 public static void BuildXML(string strXML)
115 {
116 StreamWriter sw=new StreamWriter(strXML);
117 try
118 {
119 sw.WriteLine(strFirst+strLast);
120 }
121 catch(Exception ex)
122 {
123 throw new Exception(ex.Message);
124 }
125 finally
126 {
127 sw.Close();
128 }
129 }
130 }
131}
132
2using System.IO;
3using System.Xml;
4
5namespace RWXML
6{
7 /// <summary>
8 /// TransXSD 的摘要说明。
9 /// </summary>
10 public class TransXSD
11 {
12 public static string strFirst="<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n\r";
13 public static string strLast="";
14 public static bool result=false;
15 public TransXSD()
16 {
17 //
18 // TODO: 在此处添加构造函数逻辑
19 //
20 }
21 /// <summary>
22 /// Check the childnodes if exist element "xs:element"
23 /// </summary>
24 /// <param name="node"></param>
25 /// <returns></returns>
26 public static bool CheckNode(XmlNode node)
27 {
28 foreach(XmlNode nodeC in node.ChildNodes)
29 {
30 if(nodeC.Name=="xs:element")
31 {
32 result=true;
33 }
34 else
35 {
36 if(nodeC.ChildNodes.Count>0)
37 {
38 CheckNode(nodeC);
39 }
40 }
41 }
42 return result;
43 }
44 public static void InitialResult()
45 {
46 result=false;
47 }
48 /// <summary>
49 /// Get the xml text
50 /// </summary>
51 /// <param name="node"></param>
52 public static void GetNodes(XmlNode node)
53 {
54 if(node.HasChildNodes)
55 {
56 if(node.Name=="xs:element")
57 {
58 if(CheckNode(node))
59 {
60 InitialResult();
61 strFirst+="<"+node.Attributes["name"].InnerXml+">\n\r";
62 }
63 else
64 {
65 string temp1="";
66 temp1+="<"+node.Attributes["name"].InnerXml+">"+"</"+node.Attributes["name"].InnerXml+">\n\r";
67 strFirst+=temp1;
68 }
69
70 }
71 if(CheckNode(node)==true)
72 {
73 foreach(XmlNode nodeC in node.ChildNodes)
74 {
75 InitialResult();
76 GetNodes(nodeC);
77 }
78 if(node.Name=="xs:element")
79 {
80 strFirst+="</"+node.Attributes["name"].InnerXml+">\n\r";
81 }
82 }
83 }
84 else
85 {
86 if(node.Name=="xs:element")
87 {
88 string temp1="";
89 temp1+="<"+node.Attributes["name"].InnerXml+">"+"</"+node.Attributes["name"].InnerXml+">\n\r";
90 strFirst+=temp1;
91 }
92 }
93 }
94 /// <summary>
95 /// Build the text for xml from *.xsd
96 /// </summary>
97 /// <param name="strXSD">The *.xsd file address</param>
98 /// <param name="strXML">The xml stord address</param>
99 public static void BuildTextandXML(string strXSD,string strXML)
100 {
101 BuildText(strXSD);
102 BuildXML(strXML);
103 }
104 public static void BuildText(string strXSD)
105 {
106 XmlDocument doc=new XmlDocument();
107 doc.Load(strXSD);
108 XmlNodeList list=doc.DocumentElement.ChildNodes;
109 foreach(XmlNode node in list)
110 {
111 GetNodes(node);
112 }
113 }
114 public static void BuildXML(string strXML)
115 {
116 StreamWriter sw=new StreamWriter(strXML);
117 try
118 {
119 sw.WriteLine(strFirst+strLast);
120 }
121 catch(Exception ex)
122 {
123 throw new Exception(ex.Message);
124 }
125 finally
126 {
127 sw.Close();
128 }
129 }
130 }
131}
132
阿里云优惠券领取地址:https://partner.aliyun.com/shop/35783975 ,需要更多优惠请联系QQ和微信:891115,热线:400-968-4968