现在有很多的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);
}
}
return elements;
}
/**//// <summary>
/// 添加元素
/// </summary>
/// <param name="sourcexsd"></param>
/// <param name="titles"></param>
/// <param name="sourcexml"></param>
/// <param name="sourcend"></param>
/// <param name="values"></param>
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;
}
}
/**//// <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;
}
}
新闻热点
疑难解答
图片精选