現在有很多的xml工具軟件都能根據xsd文件書寫出XML文檔,.Net 沒有實現此方法,如是我寫了幾個浏覽、校驗、和創建XML的方法
全部代碼如下:
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>
/// ProXML 的摘要說明
/// </summary>
public class ProXML
{
public ProXML()
{
//
// TODO: 在此處添加構造函數邏輯
//
}
/**//// <summary>
/// 獲得xsd文件路徑
/// </summary>
public static string GetSchemaPath
{
get{
return HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\\system\\publish.xsd";
}
}
/**//// <summary>
/// 獲理節點
/// </summary>
/// <returns></returns>
public static System.Collections.Generic.List<XMLSchemaElement> 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<XmlSchemaElement> elements=new System.Collections.Generic.List<XMLSchemaElement> ();
foreach (XMLSchemaObject obj in schema.Elements.Values)
{
if (obj.GetType() == typeof(XMLSchemaElement))
{
elements.Add((XMLSchemaElement)obj);
}
; }
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;
}
}
/**//// <summary>
/// 獲得元素
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
public static 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 static 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>
/// 根據節點名獲得節點
/// </summary>
/// <param name="name"></param>
/// <returns></returns>
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;
}
}
/**//// <summary>
/// 校驗dom對象
/// </summary>
/// <param name="doc"></param>
/// <returns></returns>
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;
}
}