XSD文件生成C#VO實體類,
最近公司要做一個項目,需要和現有的其他項目對接,由於不知道他們的數據庫,只有XSD文件。所以,我們在修改相應的程序時,就需要根據他們提供的XSD文件,來寫我們的VO實體類,由於我寫過根據Oracle數據庫生成VO實體類,因此這次的這個活也就很自然的落在了我的頭上。
一、XSD
首先什麼是XSD,我就不解釋了,因為我也不是很清楚,第一次接觸,具體的詳解度娘一下很多,XSD是XML Schema Definition 的簡稱,既然是XML,那讀取方式,就按照XML的方式就可以了。
二、解析的XSD文件
我要解析的XSD文件是如下這個樣子的,大家可以看到這個這個文件還需要加載兩個xsd文件,但是通過我們的分析,對於我們這個項目來說,如果要生成實體類,除了需要解析下面這個圖示XSD以外,還需要解析一個type類型xsd,也就是圖中include的第二個xsd文件。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483361.png)
要解析的XSD
先來看看上面這個VO實體類文件,通過截圖,可以看出來,這個是一個VO實體類包含了好多個字段,但是每個字段的type又沒有表示出來,因此需要到下面這個截圖的xsd裡去尋找,因為下圖中的name對應著上圖中的type,而下圖中的base正是我們需要的字段類型。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483367.png)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
<xs:simpleType name="acbfyhdcbfyze">
<xs:annotation>
<xs:documentation>按成本費用核定成本費用總額</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:double"/>
</xs:simpleType>
<xs:simpleType name="acbfyhdhdlrl">
<xs:annotation>
<xs:documentation>按成本費用核定核定的利潤率</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:double"/>
</xs:simpleType>
<xs:simpleType name="acbfyhdhsdsre">
<xs:annotation>
<xs:documentation>按成本費用核定換算的收入額</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:double"/>
</xs:simpleType>
<xs:simpleType name="acbfyhdynssde">
<xs:annotation>
<xs:documentation>按成本費用核定應納稅所得額</xs:documentation>
</xs:annotation>
<xs:restriction base="xs:double"/>
</xs:simpleType>
基礎XSD
三、分析和解析
通過分析上面的兩個文件,由於要生成實體類文件,因此要解析的文件需要讀取成功以後,生成一個List列表,而基礎XSD則應該解析成一個Dictionary,而這個Dictionary中只需要包含name和base,然後通過循環List列表,依次進Dictionary中進行相應的替換,就可以得到字段的類型,從而生成一個最終需要的List集合。
有了分析以後,那就是付諸實踐了,首先要能解析XSD文件,解析XSD和解析XML差不多,就是通過獲取節點集合,然後循環得到自己想要的東西。
1、根據節點設置靜態字段,方便在解析的時候進行調用
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
public static class ConstField
{
public const string Schema = "xs:schema";
public const string ComplexType = "xs:complexType";
public const string Element = "xs:element";
public const string Annotation = "xs:annotation";
public const string Documentation = "xs:documentation";
public const string Sequence = "xs:sequence";
public const string SimpleType = "xs:simpleType";
public const string Restriction = "xs:restriction";
}
靜態字段
2、寫一個實體類,其中包含name(字段名),type(字段類型),note(注釋),為了生存List時用
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
public class AnalysisVo
{
private string name;
private string note;
private string type;
private List<AnalysisVo> children;
/// <summary>
/// 名稱
/// </summary>
public string Name
{
get
{
return name;
}
set
{
name = value;
}
}
/// <summary>
/// 備注
/// </summary>
public string Note
{
get
{
return note;
}
set
{
note = value;
}
}
/// <summary>
/// 類型
/// </summary>
public string Type
{
get
{
return type;
}
set
{
type = value;
}
}
/// <summary>
/// 子集
/// </summary>
public List<AnalysisVo> Children
{
get
{
return children;
}
set
{
children = value;
}
}
}
實體類
3、解析XSD文件
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
public void Analysis()
{
XmlElement rootElement = _document.DocumentElement;//獲取根節點
XmlNodeList complexTypeNodes = rootElement.GetElementsByTagName(ConstField.ComplexType);//獲取complexType子節點集合
foreach (XmlNode complexTypeNode in complexTypeNodes)
{
AnalysisVo vo = new AnalysisVo();
XmlElement voElement = (XmlElement)complexTypeNode;
string nameVo = voElement.GetAttribute("name");//獲取類的名字
string noteVo = voElement.FirstChild.InnerText;//獲取類的注釋
List<AnalysisVo> childrenList = new List<AnalysisVo>();
XmlNodeList elementNodes = voElement.GetElementsByTagName(ConstField.Element);//獲取element子節點集合
foreach (XmlNode elementNode in elementNodes)
{
AnalysisVo childVo = new AnalysisVo();
XmlElement fieldElement = (XmlElement)elementNode;
string nameField = fieldElement.GetAttribute("name");//獲取字段名字
string typeField = fieldElement.GetAttribute("type");//獲取字段類型
string noteField = ""; //獲取字段注釋
if(fieldElement.FirstChild!=null)
noteField= fieldElement.FirstChild.InnerText;
childVo.Name = nameField;
childVo.Type = typeField;
childVo.Note = noteField;
childrenList.Add(childVo);
}
vo.Name = nameVo;
vo.Note = noteVo;
vo.Children = childrenList;
VoList.Add(vo);
}
}
解析實體類XSD文件
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
public void Analysis()
{
XmlElement rootElement = _document.DocumentElement;//獲取根結點
XmlNodeList simpleTypeNodes = rootElement.GetElementsByTagName(ConstField.SimpleType);//獲取simpleType子節點集合
foreach(XmlNode simpleTypeNode in simpleTypeNodes)
{
XmlElement dicElement = (XmlElement)simpleTypeNode;
string dicKey = dicElement.GetAttribute("name");//獲取類型名字
string dicValue = ((XmlElement)dicElement.LastChild).GetAttribute("base");//獲取類型內容
dicValue = dicValue.Replace("xs:", "");
DicType.Add(dicKey, dicValue);
}
}
解析Type類型XSD
4、將以上得到的兩個文件進行整合,從而生成一個最終的List文件
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483403.gif)
![]()
private void ReplaceContent(AnalysisVo node)
{
//正常在基礎的字段表中存在
if (DicType.ContainsKey(node.Type))
{
node.Type = DicType[node.Type];
}//要解析的裡面沒有type字段,只有name字段 yanc 20160304
else if (string.IsNullOrEmpty(node.Type) && DicType.ContainsKey(node.Name))
{
node.Type = DicType[node.Name];
}
else//List類型的沒有存儲在基礎的字段表中,需要循環遍歷本身的List列表
if(!string.IsNullOrEmpty(node.Type)&&(node.Type.Substring(node.Type.Length - 4, 4).Equals("Grid") || node.Type.Substring(node.Type.Length - 4, 4).Equals("List")))
{
foreach (var root in VoList)
{
if (root.Name.Equals(node.Type))
{
node.Type = root.Children.First().Type;
}
}
}//要解析的裡面沒有type字段,只有name字段 yanc 20160304
else if(string.IsNullOrEmpty(node.Type) && (node.Name.Substring(node.Name.Length - 4, 4).Equals("Grid") || node.Name.Substring(node.Name.Length - 4, 4).Equals("List")))
{
foreach (var root in VoList)
{
if (root.Name.Equals(node.Name))
{
node.Type = root.Children.First().Name;
}
}
}
else
{
Console.WriteLine("未能轉化的內容" + node.Name);
}
}
View Code
在替換的過程中,會有一種情況,即一個實體類中包含List<>泛型集合,我相信大家在開發過程中,見過這種情況,因此,在替換的時候,需要判斷一下,通過我們的分析,一般這種的字段type是在基礎類中找不到的,而是在當前的文件中進行查找,因此,當遇到的時候,還需要再次循環一遍自身集合,從而找到實體集合,將他的Name屬性賦值給當前的Type字段。
四、生成CS文件
最後一步,就是生成CS文件了,和我上一篇文章大同小異,不過有一點需要注意,因為數據庫生成的實體類是沒有List類型的,但是這個XSD生成的有,因此需要稍加變動一下生成方法。
注:以上解析方法,同樣適用於下圖的一般request和response文件,特殊的需要另當處理。
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483419.png)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483423.png)
五、Demo效果圖
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483469.png)
一個XSD生成了多個VO文件,每一個VO就是一個實體類
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483431.png)
![](https://www.aspphp.online/bianchen/UploadFiles_4619/201701/2017012017483407.png)
代碼在GitHub上,希望大家一起幫忙修改,畢竟foreach嵌套foreach效率不是很高。