根据xsd生成xml文档

2008-02-22 09:39:51来源:互联网 阅读 ()

新老客户大回馈,云服务器低至5折

现在有很多的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)

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:获取数据行数据信息

下一篇:用MasterPage 代替 PageBase