現在有很多的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;
/**////
/// ProXML 的摘要說明
///
public class ProXml
{
public ProXml()
{
//
// TODO: 在此處新增建構函式邏輯
//
}
/**////
/// 取得xsd檔案路徑
///
public static string GetSchemaPath
{
get{
return HttpContext.Current.Request.PhysicalApplicationPath + "App_Data\system\publish.xsd";
}
}
/**////
/// 獲理節點
///
///
public static System.Collections.Generic.List
{
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
foreach (XmlSchemaObject obj in schema.Elements.Values)
{
if (obj.GetType() == typeof(XmlSchemaElement))
{
elements.Add((XmlSchemaElement)obj);
}
}
return elements;
}
/**////
/// 新增元素
///
///
///
///
///
///
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;
}
}
/**////
/// 取得元素
///
///
///
public static System.Collections.Generic.List
{
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
{
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;
}
}
/**////
/// 根據節點名獲得節點
///
///
///
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;
}
}
/**////
/// 校驗dom對象
///
///
///
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;
}
}
http://www.cnblogs.com/eric812/archive/2006/11/01/546914.html